Re: Can not create a Vector of Strings
Hi,
I'll inline my 2 cent.
Thomas
arnuld wrote:
Hi Friends,
I hope you have remembered me. Its been a long time since I hung here at
comp.lang.c++. I still remember names like Daniel T., Victor Bazarov,
Erik Wikstrom and many others :) .Good news is that I got a job and I am
earning money and no longer dependent on my old parents. Bad news is that
in these last 6 months , the job which required me to learn C and Socket
Programming, nearly ate all of my time everyday. so I have not touched the
C++ and forgotten the style and feel of it.
Now today, I have started to walk again on C++ and crated a small program.
I can't compile it anyway:
// simple illustration of Std. Lib. Vector
// this program will ask user to input a single word and will store that input
// into a vector and will print it when user have hit the EOF or entered 3 words
#include <iostream>
#include <string>
#include <vector>
int main()
{
const int svec_size = 3;
I suspect that you really wanted to use one vector instead of a array of
vectors:
std::vector<std::string> svec(svec_size);
std::vector<std::string> svec[svec_size];
std::string user_input;
If you want the for loop to exit after 'svec_size' elements you should
do something like this:
for( int i = 0; (std::cin >> user_input) && (i != svec_size); ++i )
But I don't know enough about iostreams to know when '(std::cin >>
user_input)' will return false. In fact I always thought that it
returned a std::istream reference so you might want to read up on it.
for( int i = 0; (std::cin >> user_input) || (i != svec_size); ++i )
{
svec.push_back( user_input );
}
There is a typo below e.g. it should have been 'std::cout'. But the
compiler probably gave you a warning about it.
std:cout << "-------------------------" << std::endl;
for( std::vector<std::string>::const_iterator citer=svec.begin();
citer != svec.end(); ++citer )
{
std::cout << *citer;
}
return 0;
}
================= OUTPUT =========================
[arnuld@dune ztest]$ g++ -ansi -pedantic -Wall -Wextra second.cpp
second.cpp: In function `int main()':
second.cpp:19: error: request for member `push_back' in `svec',
which is of non-class type `std::vector<std::string,
std::allocator<std::string> >[3]' second.cpp:22: error: `cout' was not
declared in this scope second.cpp:24: error: request for member `begin' in
`svec', which is of non-class type `std::vector<std::string,
std::allocator<std::string> >[3]' second.cpp:25: error: request for member
`end' in `svec', which is of non-class type `std::vector<std::string,
std::allocator<std::string> >[3]' second.cpp:22: warning: unused variable
'cout' second.cpp:22: warning: label `std' defined but not used
[arnuld@dune ztest]$