Reputation: 3
C++ code
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <fstream>
using namespace std;
int
main ()
{
vector < string > elem (4);
ifstream infile ("test.txt");
if (!infile)
{
cerr << "Oops! unable to save session data !\n";
}
int i;
for (i = 0; infile >> elem[i]; i++);
for (i = 0; i < 4; i++)
{
cout << elem[i] << ' ';
}
cout << "\nThe result after sorting\n";
sort (elem, elem + 4);
for (i = 0; i < 4; i++)
{
cout << elem[i] << ' ';
}
cout << endl;
return 0;
}
THis is my full code,when I run,there is some problem
main.cpp: In function ‘int main()’:
main.cpp:24:20: error: no match
for ‘operator+’ (operand types are ‘std::vector >’ and ‘int’) sort
(elem, elem + x);
~~~~~^~~
Upvotes: 0
Views: 141
Reputation: 11801
Your usage of std::sort
is C-ish. You have to supply iterators to begin/end, i.e., write something like
sort(elem.begin(), elem.end());
Besides, your program will crash if the test.txt
file is more than 4 lines (trying to read into non-existent elem[4]
).
Be careful though, check that the default comparison does what you want. Here it does.
Upvotes: 3