using string in vector

closed account (oLC9216C)
I am trying to use string in vector, but an error comes out.
error: no match for 'operator>>' in 'fin >> text'|

1
2
3
4
5
6
7
8
9
10
    ifstream fin;
    fin.open("address.xml");
    vector<string> text;
    while(!fin.eof())
    {

        fin >> text;

    }
    cout << text;

You can't extract a full vector like that.

You have to extract the strings one at a time and insert them into your vector.
closed account (oLC9216C)
So what I exactly have to do?
1
2
3
4
5
6
7
8
9
10
11
  ifstream fin;
    fin.open("address.xml");
    vector<string> text;
    while(!fin.eof())
    {
         for(int i= 0; i < text.size(); ; i++)
         {
             fin >> text.at(i);
          }
    }
    cout << text;


try this out
Last edited on
@pilla: that won't work because text.size() is zero.

@simon: Read individual strings from the file and then push_back them into the vector:

1
2
3
4
5
string temp;
while( fin >> temp )
{
  text.push_back( temp );
}
closed account (oLC9216C)
@disch

i tired, and this comes out
error: no match for 'operator>>' in 'fin >> text'|
also how to use cout?
Last edited on
vectors use [brackets] to address their elements, not (parenthesis)

cout << text[i];
Last edited on
i tired, and this comes out
error: no match for 'operator>>' in 'fin >> text'|


You mistyped what I wrote. I said fin >> temp... not fin >> text
closed account (oLC9216C)
what I did is
1
2
3
4
5
6
7
8
9
    ifstream fin;
    fin.open("address.xml");
    vector<string> text;
    while( fin >> text )
    {
    text.push_back( text );


    }


@while( fin >> text ) is the error
Yes, I know. That's not what I typed.

Reread my last post.

EDIT: To clarify.. you need another string, which I named "temp". This is different from your vector, which is named "text".
Last edited on
closed account (oLC9216C)
Finally it works, thank you for the help.
Topic archived. No new replies allowed.