-
STL lists
Compiled the code on g++ and it won't work, why?
Code:
#include <iostream>
#include <string>
#include <list>
#include <algorithm>
using namespace std;
void func(string t) {
cout << t << endl;
}
int main() {
list < string > prod_code;
string temp;
do {
cout << "Enter Product Code: ";
if (cin >> temp) {
prod_code.push_back(temp);
} else {
break;
}
} while (1);
sort( prod_code.begin(), prod_code.end() );
for_each(prod_code.begin(), prod_code.end() , func);
return 0;
}
-
The compile won't work..? Least, I couldn't get it to go, but I modfied it a bit and it was all working fine.
Code:
#include <iostream>
#include <string>
#include <list>
#include <algorithm>
using namespace std;
void func(string t) {
cout << t << endl;
}
int main() {
list < string > prod_code;
string temp;
do {
cout << "Enter Product Code: ";
if (cin >> temp) {
// couldn't figure out how to make cin >> temp fail without exitting program :(
if (temp != "stop") prod_code.push_back(temp);
else break;
} else {
break;
}
} while (1);
// sort( prod_code.begin(), prod_code.end() ); // didn't compile with gcc 2.9.5 or 3.0.4
// list's have their own special sort (std::uglyAndHackish :( ) because they don't have random access iterators
// which makes the default (usual quicksort) simply no longer quick :).
prod_code.sort();
for_each(prod_code.begin(), prod_code.end() , func);
return 0;
}
-
Thanks heaps for your help, greatly appreciated. I compiled the code and it works. Thank you.