can any one tell how to scan a
vector<string> v ;
cout<<"Enter a string"<<endl;
cin>>v; // ???? how to scan a string into vector ......is there another way to scan
it
You can choose to implement your own vector class that contains string. Then overload the operator>>() for convenience. But re-implementing the vector class itself is overkill. I would suggest the same as what ropez did.
You don't need to reimplement the vector class to implement an input stream operator, you can do it simply like this:
1 2 3 4 5 6
std::istream& operator>>(std::istream& is, std::vector<std::string>& vec) {
string str;
is >> str;
vec.push_back(str);
return is;
}
Then the code in your original post would work. Howver, the reason I didn't suggest this in the first place was that I don't think the behavior of this operator is obvious. Someone might think that "cin >> vec" would mean e.g. to read all words in the current line, and replace the contents of the vector with these words. I think it's good practice to only implement operators when the behavior is obvious.
Note: The operator above is for vectors containing strings only. It is also possible to declare the operator as a template, so that it can be used for vectors containing any datatype for which the input stream operator is defined. I used string explicitly in this example for simplicity.