How do I get the number's in order but not changing the name it goes with?
SO here is how I started it
# include <iostream>
# include <fstream>
# include <sstream>
# include <set>
# include <list>
# include <string>
# include <cctype>
# include <vector>
using namespace std;
int main()
{
fstream ifile("who.txt");
if (!ifile.good())
{
cout<<" Can't open the file"<<endl;
}
string header;
getline (ifile, header);
cout<<header<<endl;
string line;
string term;
while (getline (ifile,line))
{
istringstream iss(line);
string p;
iss >> p;
vector<string> terms;
string m;
string value;
for (vector<string>::const_iterator iter = terms.begin(); iter != terms.end(); ++iter)
{
cout << *iter<< ", ";
}
cout << endl;
}
ifile.close();
return 0;
}
#include <iostream> //cout - main
#include <string> //string - main ,class and func
#include <algorithm> //reverse, sort, greater - class
#include <vector> //vector - main and func
#include <sstream> //stringstream - main
usingnamespace std;
class Person
{
private:
string name_;
unsigned num_;
public:
Person(string raw)
{
this->num_ = stoi(raw);
reverse(raw.begin(), raw.end()); //Those "reverse" are for getting only the number and the string
raw = raw.substr(0, raw.find(' '));
reverse(raw.begin(), raw.end()); //Ends here :P
this->name_ = raw;
}
string name() {returnthis->name_;}
unsigned num() {returnthis->num_;}
};
vector<Person> order_vector(vector<Person>& rawPerson)
{
//Ok, lets check which one is the first
vector<Person> ret;
vector<unsigned> values;
for(unsigned i = 0; i < rawPerson.size(); i++) values.push_back(rawPerson[i].num());
sort(values.begin(), values.end(), greater<unsigned>());
for(unsigned a = 0; a < rawPerson.size(); a++)
for(unsigned i = 0; i < rawPerson.size(); i++)
{
if(values[a] == rawPerson[i].num()) ret.push_back(rawPerson[i]);
}
return ret;
}
int main()
{
string raw_strings;
vector<Person> rawPerson;
vector<Person> ordened_Person;
stringstream rawstring(string("10 tennent\n9 Eccleston\n12 Capaldi\n11 Smith"));
while(getline(rawstring, raw_strings)) //get info before '\n'
{
if(!raw_strings.empty()) //If it is not an '\n'
{
rawPerson.push_back(Person(raw_strings)); // add an object
}
}
ordened_Person = order_vector(rawPerson);
for(auto& a : ordened_Person) std::cout << a.num() << '\t' << a.name() << std::endl;
}
#include <iostream> //cout - main
#include <string> //string - main ,class
#include <algorithm> //reverse, sort, greater - class and main
#include <vector> //vector - main
#include <sstream> //stringstream - main
usingnamespace std;
class Person
{
private:
string name_;
unsigned num_;
public:
Person(string raw)
{
this->num_ = stoi(raw);
reverse(raw.begin(), raw.end());
raw = raw.substr(0, raw.find(' '));
reverse(raw.begin(), raw.end());
this->name_ = raw;
}
string name() {returnthis->name_;}
unsigned num() {returnthis->num_;}
booloperator<(const Person& person) const
{
return num_ < person.num_;
} //No order_vector
};
int main()
{
string raw_strings;
vector<Person> ordened_Person; //No raw_person
stringstream rawstring(string("10 tennent\n9 Eccleston\n12 Capaldi\n11 Smith"));
while(getline(rawstring, raw_strings)) //get info before '\n'
{
if(!raw_strings.empty()) //If it is not an '\n'
{
ordened_Person.push_back(Person(raw_strings)); // add an object
}
}
sort(ordened_Person.begin(), ordened_Person.end());
for(auto& a : ordened_Person) std::cout << a.num() << '\t' << a.name() << std::endl;
}
PS: C++ 2011
Oh, if you want it descending, after sort(ordened_Person.begin(), ordened_Person.end());, write reverse(ordened_Person.begin(), ordened_Person.end());.
Compile it with