Use a set, not a vector, if you want your string entries sorted.
Code:
#include <set>
#include <iterator>
#include <iostream>
int main()
{
using namespace std;
set<string> myStrings;
myStrings.insert("fum");
myStrings.insert("fee");
myStrings.insert("fi");
myStrings.insert("fo");
copy(myStrings.begin(), myStrings.end(), ostream_iterator<string>(cout, "\n"));
}
If you must use a vector, then I "smell" homework.
As for your do-while loop, there's an issue there as well:
Code:
do
{
textFile1 >> insertion[count1];
count1 ++;
}while(!textFile1.eof());
Do not attempt to insert into your container until you know that you have a valid entry to insert. eof() will NOT return true until you have actually attempted to read past the end of the file. Thus if the last entry in the file has successfully been read, then eof() will report false. It will take another read (and it will be unsuccessful) for eof() to return true.
This may be a better style loop to use:
Code:
string s;
while (textFile1 >> s)
{
// do something with s
}
P.S. If you anticipate having duplicate strings, then use a multiset.
Bookmarks