[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Can not create a Vector of Strings

arnuld

9/9/2008 8:03:00 AM

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

10 Answers

Ian Collins

9/9/2008 8:08:00 AM

0

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.

Fred Zwarts

9/9/2008 8:13:00 AM

0

"arnuld" <sunrise@invalid.address> wrote in message news:pan.2008.09.09.08.02.52.279850@invalid.address...
> 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
>

Thomas Austad

9/9/2008 8:40:00 AM

0

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

arnuld

9/9/2008 9:21:00 AM

0

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


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

arnuld

9/9/2008 9:30:00 AM

0

> 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>
>>::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;
}



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

Nick Keighley

9/9/2008 9:36:00 AM

0

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>>::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


--
Nick Keighley

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

pjb

9/9/2008 9:40:00 AM

0

arnuld <sunrise@invalid.address> 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::push_back will take a *character* and append it to the string.

--
__Pascal Bourguignon__

Erik Wikström

9/9/2008 5:23:00 PM

0

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

--
Erik Wikström

Pranav

9/10/2008 6:21:00 AM

0

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

James Kanze

9/10/2008 9:08:00 AM

0

On Sep 9, 7:23 pm, Erik Wikström <Erik-wikst...@telia.com> wrote:
> On 2008-09-09 11:30, arnuld wrote:

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

> 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.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34