i don't even understand this line of code that u've sent. can u elaborate. what is a vector anyway?
A vector is a container class from the C++ Standard Template library (STL). All you need to know for practical use, however, is that a vector can be used to store a sequence of values of a certain type, that it grows automatically if you need more space and that it grants efficient access. It's similar to a simple array, but somewhat more comfortable.
For example,
-
vector<string> vec_of_str;
-
vector<int> vec_of_int;
-
vector<double> vec_of_dbl;
-
define vectors that hold elements of type string, int and double, respectively.
BTW, string is also a type from the STL and no built-in type like int or float.
Please refer also to
http://www.cppreference.com to get an idea what things you can do with vectors and strings.
Here comes the code that may do what you need. Have a look and try to understand it (I added some comments :))
-
#include <iostream>
-
#include <fstream>
-
#include <string>
-
#include <vector>
-
-
using namespace std;
-
-
int main( void )
-
{
-
// define an infile stream
-
ifstream fin( "textfile.txt" );
-
-
// define a vector of strings
-
vector<string> sentences;
-
-
//define a temporary string
-
string tmp_str;
-
-
// read the file line by line into tmp_str
-
while( getline( fin, tmp_str ) ) {
-
-
// append the tmp_str as a new last
-
// element to sentences
-
sentences.push_back( tmp_str );
-
}
-
-
// print out the 3rd element of our vector
-
cout << sentences[2];
-
-
return 0;
-
}
-