Question:
C++ help with matching sorted vector of doubles with vec of strings?
2009-04-30 07:46:10 UTC
How can I sort the vector of doubles matching the vector of strings

So if I input
A 100
Z 200
B 54
D 123
J 123

it should output
A 100
B 54
D 123
J 123
Z 200

#include
#include
#include
#include
int main()
{
std::vector age;
std::vector name;
std::string s;
while(std::cin >> s) name.push_back(s);
double d = 0;
for(int i = 0; i < name.size(); ++i) {std::cin >> d; age.push_back(d);}

std::sort(name.begin(),name.end());

// Sort the age vector that they match the sorted string
}


Thanks
Four answers:
2009-04-30 08:38:34 UTC
Hi,



Hope you can understand this. Just run it and see how it works.



#include

#include

#include

#include



using namespace std;



struct nameAndAge{

double age;

string name;

};



bool compare(nameAndAge a, nameAndAge b){

return (a.name < b.name);

}



int main()

{

vector details;



// assuming there are 5 names and 5 age groups and they corresond to each other



string s[] = {"A", "Z", "B", "D", "J"};

double d[] = {100, 200, 54, 123, 123};



for(int i = 0; i < 5; i++){

nameAndAge q;

q.age = d[i];

q.name = s[i];

details.push_back(q);

}



sort(details.begin(), details.end(), compare);

for(int i = 0; i < details.size(); i++){

cout<
}



}
cja
2009-04-30 08:26:12 UTC
Your code is confusing. I think it would be better if you kept your { name, age } pairs together, in one vector, and sorted that. It may not be exactly what you have in mind, but that's what I would do. Here's how:



#include

#include

#include

#include



using namespace std;



struct StInfo {

    string _name;

    float _age;

    StInfo() : _name(""), _age(0.0) { }

    StInfo(string name, float age) : _name(name), _age(age) { }

    StInfo(const StInfo &s1) {

        _name = s1._name;

        _age = s1._age;

    }

    bool operator() (const StInfo &s1, const StInfo &s2) {

        return ((s1._name < s2._name) ||

                        ((s1._name == s2._name) &&

                          (s1._age < s2._age)));

    }

};



istream &operator>>(istream &is, StInfo &st) {

    is >> st._name;

    is >> st._age;

    return is;

}



void print(const vector&);



int main(int argc, char *argv[]) {

    vector sVec;

    struct StInfo sObj;

    ifstream infile;



    if (argc > 1) {

        ifstream infile(argv[1]);

        if (infile.is_open()) {

            while (infile) {

                if (infile >> sObj) {

                    sVec.push_back(StInfo(sObj));

                }

            }

            infile.close();

            print(sVec);

            sort(sVec.begin(),sVec.end(),sObj);

            cout << endl << "Sorted: " << endl;

            print(sVec);

        } else {

            cout << argv[1] << " not found." << endl;

            return -1;

        }

    } else {

        cout << "usage: " << argv[0] << " " << endl;

        return -1;

    }

    return 0;

}



void print(const vector &v) {

    vector::const_iterator i;

    for (i = v.begin(); i != v.end(); i++) {

        cout << i->_name << "," << i->_age << endl;



    }

}





Sample run:



$ ./sortTst sortTst.dat

A,100

Z,200

B,55

B,54

D,123

J,123



Sorted:

A,100

B,54

B,55

D,123

J,123

Z,200





Where:

$ cat sortTst.dat

A 100

Z 200

B 55

B 54

D 123

J 123
phillippejr
2016-10-04 03:32:40 UTC
i can't remember any on the 2d. My answer would be India vs Australia, Sydney try 2008 = umpiring under the scanner after some extreme profile errors. & New Zealand vs Australia = Melbourne ODI, 1981 = infamous underarm final ball by employing Trevor Chappel while NZ necessary 6 runs.
2014-11-06 21:12:54 UTC
complex matter. look over google. that can help!


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...