简体   繁体   中英

How can ı sort string and its corresponding integer in c++?

Hello ı have a string vector (favseries) which includes actornames. and ı created three vector to store the count (vector counts), store the actors (vector actors), store the actors provided each actor be once (vector uniqueactors);ı want to find actorname's counts and display each actornames with their counts with sorted counts high to low, if counts are same, sorted strings from "A" to "Z".

my string vector includes:

WENTWORTH MILLER  FREYA ALLAN  YASEN ATOUR  BASIL EIDENBENZ  ANYA CHALOTRA  
BRYAN CRANSTON  RAINN WILSON  WENTWORTH MILLER  LESLIE DAVID BAKER  BRYAN CRANSTON  ANYA CHALOTRA YASEN ATOUR 

here is the output must be:

ANYA CHALOTRA: 2
BRYAN CRANSTON: 2
WENTWORTH MILLER: 2
YASEN ATOUR: 2
BASIL EIDENBENZ: 1
FREYA ALLAN: 1
LESLIE DAVID BAKER: 1
RAINN WILSON: 1

my output is error called vector out of range in the sort part. also my output when does not give any error, it prints like no sorted string according to 0'th index and one actor was printed two times with count 1. for example:

ANYA CHALOTRA: 1
BRYAN CRANSTON: 1
WENTWORTH MILLER: 1
YASEN ATOUR: 1
BASIL EIDENBENZ: 1
YASEN ATOUR: 1
ANYA CHALOTRA: 1
BRYAN CRANSTON: 1
WENTWORTH MILLER: 1
FREYA ALLAN: 1
LESLIE DAVID BAKER: 1
RAINN WILSON: 1

here is the code:

    vector <string> actors;
    vector <int> counts;
    vector <string> uniqueactors;
    int count;
    for ( int i = 0; i < favSeries.size(); i++) { 
        for ( int j = 0; j < favSeries[i].actorNames.size(); j++) {
            count = 1;
            counts.push_back(count);
            actors.push_back(favSeries[i].actorNames[j]);
        }
    }
    uniqueactors.push_back(actors[0]);
    for ( int i = 0; i < actors.size(); i++ ) {
        bool check = true;
        for ( int j = 0; j < uniqueactors.size(); j++ ) {
            if ( uniqueactors[j] == actors[i] ) {
                counts[j] += 1;
                check = false;
            }
        }
        if (check) {
            uniqueactors.push_back(actors[i]);
        }
    }

    for ( int i = 0; i < uniqueactors.size() - 1; i++) {
    int minindex = i;
        for (int j = i + 1; j < uniqueactors.size(); j++) {
            if  (actors[minindex].at(0) > uniqueactors[j].at(0)) {
                minindex = j; 
            }
        }
        string temp = uniqueactors[i];
        uniqueactors[i] = uniqueactors[minindex];
        uniqueactors[minindex] = temp;
        int temp1 = counts[i];
        counts[i] = counts[minindex];
        counts[minindex] = temp1;
    }
    for ( int i = 0; i < counts.size() - 1; i++) {
        int minindex = i;
        for (int j = i + 1; j < counts.size(); j++) {
            if( counts[minindex] > counts[j] ) {
                minindex = j;
            }
        }
        int temp1 = counts[i];
        counts[i] = counts[minindex];
        counts[minindex] = temp1;
        string temp = uniqueactors[i];
        uniqueactors[i] = uniqueactors[minindex];
        uniqueactors[minindex] = temp;
    }
    for ( int i = 0; i < uniqueactors.size(); i++ ) {
        cout << uniqueactors[i] << ":" << count << endl;
    }
}

or can you rearrange vector and sorting? thank you...

You can use sort() from algorithm in stl.

vector<int> v;
sort(v.begin(), v.end(), [](int a, int b){
    return a < b;
})

Similar to this, you can easily implement this for different arrays. Read more about sort() .

I would also advise you to use struct or a pair .

Once you store the string and the count in a pair, here is the code:

vector<pair<string, int>> v;
sort(v.begin(), v.end(), [](auto a, auto b){
    if(a.second > b.second){
        return a.second > b.second;
    } else {
        return a.first > b.first;
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM