Category : stl

I’m currently working through Stanley Lippman’s C++ Primer. In Chapter 10 generic algorithms are introduced. As an example std::sort, std::unique and the std::vector member function erase shall be used to remove duplicate elements within a vector. To see how a vectors elements are rearranged by std::unique I tried to print every element just to find ..

Read more

#include <iostream> #include <list> #include <algorithm> #include <string> using namespace std; void out(string& str) {cout << str << " ";} int main() { list<string> lm; list<string> lp; string temp; cout << "List of Mat (quit to quit):n"; while (cin >> temp && temp != "quit") lm.push_back(temp); lm.sort(); for_each(lm.begin(), lm.end(), out); cout << endl; cout << ..

Read more

I want to pass the below function as as the third argument in sort() function from C++ STL. bool fn(int a, int b, vector<int> v1) { if (v1[a]< v1[b]) return true; else return false; } I want sort a vector according to the values in another vector. sort(v2.begin(), v2.end(),fn); How do I pass the first ..

Read more