Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Can not create a Vector of Strings

Reply
Thread Tools

Can not create a Vector of Strings

 
 
arnuld
Guest
Posts: n/a
 
      09-09-2008
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;
std::vector<std::string> svec[svec_size];
std::string user_input;

for( int i = 0; (std::cin >> user_input) || (i != svec_size); ++i )
{
svec.push_back( user_input );
}

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]$




--
www.lispmachine.wordpress.com
my email is @ the above blog.
Google Groups is Blocked. Reason: Excessive Spamming

 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      09-09-2008
arnuld wrote:

> int main()
> {
> const int svec_size = 3;
> std::vector<std::string> svec[svec_size];


Did you intend to create an array of vectors?

--
Ian Collins.
 
Reply With Quote
 
 
 
 
Fred Zwarts
Guest
Posts: n/a
 
      09-09-2008
"arnuld" <> wrote in message news s...
> 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;
> std::vector<std::string> svec[svec_size];


What do you want to do with the [svec_size]?
You are now creating an array of three vectors. Why not creating just one vector?

std::vector<std::string> svec;

> std::string user_input;
>
> for( int i = 0; (std::cin >> user_input) || (i != svec_size); ++i )
> {
> svec.push_back( user_input );


If you want to use the push_back function, you need to specify on which vector of the three vectors in the array.
You cannot push it on all vectors in the array at once.
Did you mean

svec[i].push_back( user_input );

> }
>
> std:cout << "-------------------------" << std::endl;
>
> for( std::vector<std::string>::const_iterator citer=svec.begin();
> citer != svec.end(); ++citer )


Again, the begin and end functions cannot be applied to the array of vectors.
Select one of the three vectors, or don't use an array.

> {
> 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]$
>
>
>
>
> --
> www.lispmachine.wordpress.com
> my email is @ the above blog.
> Google Groups is Blocked. Reason: Excessive Spamming
>

 
Reply With Quote
 
Thomas Austad
Guest
Posts: n/a
 
      09-09-2008
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]$
>
>
>
>

 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      09-09-2008
> On Tue, 09 Sep 2008 10:12:54 +0200, Fred Zwarts wrote:


> What do you want to do with the [svec_size]?
> You are now creating an array of three vectors. Why not creating just
> one vector?


Thats array .. oh .. no. and I thought I was giving the maximum number of
elements this vector must have. This is the new version with one error:


int main()
{
const int svec_size = 3;
std::vector<std::string> svec;
std::string user_input;

for( int i = 0; (std::cin >> user_input) || (i != svec_size); ++i )
{
svec[i].push_back( user_input );
}

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: no matching function for call to
`std::basic_string<char, std::char_traits<char>, std::allocator<char>
>:ush_back(std::string&)'

/usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/basic_string.h:795:
note: candidates are: void std::basic_string<_CharT, _Traits,
_Alloc>:ush_back(_CharT) [with _CharT = char, _Traits =
std::char_traits<char>, _Alloc = std::allocator<char>] [arnuld@dune ztest]$


Line number 19 is where i did push_back on svec.


--
www.lispmachine.wordpress.com
my email is @ the above blog.
Google Groups is Blocked. Reason: Excessive Spamming

 
Reply With Quote
 
arnuld
Guest
Posts: n/a
 
      09-09-2008
> On Tue, 09 Sep 2008 14:21:28 +0500, arnuld wrote:

> .. SNIP....


> =================== OUTPUT ===============================
> [arnuld@dune ztest]$ g++ -ansi -pedantic -Wall -Wextra second.cpp
> second.cpp: In function `int main()':
> second.cpp:19: error: no matching function for call to
> `std::basic_string<char, std::char_traits<char>, std::allocator<char>
>>:ush_back(std::string&)'

> /usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/basic_string.h:795:
> note: candidates are: void std::basic_string<_CharT, _Traits,
> _Alloc>:ush_back(_CharT) [with _CharT = char, _Traits =
> std::char_traits<char>, _Alloc = std::allocator<char>] [arnuld@dune ztest]$
>


> Line number 19 is where i did push_back on svec.



okay, I got it, it should be svec.push_back(..) . This code works fine. DO
you have any advise or views, like a I did change one thing, the type of
svec_size:



int main()
{
const std::vector<std::string>::size_type svec_size = 3;
std::vector<std::string> svec;
std::string user_input;

for( std::vector<std::string>::size_type i = 0;
(i != svec_size) && (std::cin >> user_input); ++i )
{
svec.push_back( user_input );
}

std::cout << "-------------------------" << std::endl;

for( std::vector<std::string>::const_iterator citer=svec.begin();
citer != svec.end(); ++citer )
{
std::cout << *citer << std::endl;
}

return 0;
}



--
www.lispmachine.wordpress.com
my email is @ the above blog.
Google Groups is Blocked. Reason: Excessive Spamming

 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      09-09-2008
On 9 Sep, 10:21, arnuld <sunr...@invalid.address> wrote:
> > On Tue, 09 Sep 2008 10:12:54 +0200, Fred Zwarts wrote:
> > What do you want to do with the [svec_size]?
> > You are now creating an array of three vectors. Why not creating just
> > one vector?

>
> Thats array .. oh .. no. and I thought I was giving the maximum number of
> elements this vector must have. This is the new version with one error:
>
> int main()
> {
> * const int svec_size = 3;
> * std::vector<std::string> svec;
> * std::string user_input;
>
> * for( int i = 0; (std::cin >> user_input) || (i != svec_size); ++i ) *
> * * {
> * * * svec[i].push_back( user_input );


svec[i] is an element of the vector not the vector. Ypu probably mean

svec.push_back( user_input );


> * * }
>
> *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: no matching function for call to
> `std::basic_string<char, std::char_traits<char>, *std::allocator<char>>:ush_back(std::string&)'
>
> /usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/bas*ic_string.h:795:
> note: candidates are: void std::basic_string<_CharT, _Traits,
> _Alloc>:ush_back(_CharT) [with _CharT = char, _Traits =
> std::char_traits<char>, _Alloc = std::allocator<char>] [arnuld@dune ztest]$
>
> Line number 19 is where i did push_back on svec.
>
> --www.lispmachine.wordpress.com
> my email is @ the above blog.
> Google Groups is Blocked. Reason: Excessive Spamming


so you won't see this post


--
Nick Keighley

"The Dinosaurs have come and gone,
we Theriodonts remain"

 
Reply With Quote
 
Pascal J. Bourguignon
Guest
Posts: n/a
 
      09-09-2008
arnuld <> writes:
> [...]
> std::vector<std::string> svec;
> [...]
> svec[i].push_back( user_input );
> [...]



svec is a vector of strings.
Therefore svec[i] is a string.

A string is like a vector of character.
string:ush_back will take a *character* and append it to the string.

--
__Pascal Bourguignon__
 
Reply With Quote
 
Erik Wikström
Guest
Posts: n/a
 
      09-09-2008
On 2008-09-09 11:30, arnuld wrote:
>> On Tue, 09 Sep 2008 14:21:28 +0500, arnuld wrote:

>
>> .. SNIP....

>
>> =================== OUTPUT ===============================
>> [arnuld@dune ztest]$ g++ -ansi -pedantic -Wall -Wextra second.cpp
>> second.cpp: In function `int main()':
>> second.cpp:19: error: no matching function for call to
>> `std::basic_string<char, std::char_traits<char>, std::allocator<char>
>>>:ush_back(std::string&)'

>> /usr/lib/gcc/i386-redhat-linux/3.4.6/../../../../include/c++/3.4.6/bits/basic_string.h:795:
>> note: candidates are: void std::basic_string<_CharT, _Traits,
>> _Alloc>:ush_back(_CharT) [with _CharT = char, _Traits =
>> std::char_traits<char>, _Alloc = std::allocator<char>] [arnuld@dune ztest]$
>>

>
>> Line number 19 is where i did push_back on svec.

>
>
> okay, I got it, it should be svec.push_back(..) . This code works fine. DO
> you have any advise or views, like a I did change one thing, the type of
> svec_size:
>
>
>
> int main()
> {
> const std::vector<std::string>::size_type svec_size = 3;


The type specified by size_type is very probably size_t, and unless you
are going to work with really large vectors it will probably not matter
if it is some other type. But size_t is much easier to type and read, so
I would use size_t.

--
Erik Wikström
 
Reply With Quote
 
Pranav
Guest
Posts: n/a
 
      09-10-2008
Or you may be interested in defining as,
const int svec_size = 3;
std::vector<std::string> svec(svec_size);

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Strings, Strings and Damned Strings Ben C Programming 14 06-24-2006 05:09 AM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57