Can not create a Vector of Strings

A

arnuld

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

Fred Zwarts

arnuld said:
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 said:
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.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]$
 
T

Thomas Austad

Hi,
I'll inline my 2 cent.

Thomas
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 said:
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]$
 
A

arnuld

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.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
::push_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>::push_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.
 
A

arnuld

.. 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
::push_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>::push_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;
}
 
N

Nick Keighley

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.push_back( user_input );


svec 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>>::push_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>::push_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
 
P

Pascal J. Bourguignon

arnuld said:
[...]
std::vector<std::string> svec;
[...]
svec.push_back( user_input );
[...]



svec is a vector of strings.
Therefore svec is a string.

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

Erik Wikström

.. 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
::push_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>::push_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.
 
P

Pranav

Or you may be interested in defining as,
const int svec_size = 3;
std::vector<std::string> svec(svec_size);
 
J

James Kanze

On 2008-09-09 11:30, arnuld wrote:

[...]
The type specified by size_type is very probably size_t,

The type specified by std::vector< std::string >::size_type is
required to be std::size_t. The size_type of all standard
containers comes in fact from the allocator, and the standard
allocator is required to use size_t for size_type. So unless
you're dealing with containers using custom allocators, you can
just use size_t.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top