How to initialize a vector?

Reply

Join Date: Apr 2004
Posts: 4,773
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 308
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
0
  #11
Oct 14th, 2009
“The essential notion of a socialist society is force.”
— Milton Friedman
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,583
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1614
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
-7
  #12
Oct 14th, 2009
The problem with Dave's suggestion is all those strings are now stored in memory twice -- once in the array of std::strings and again in the vector of strings. That could be a problem if the computer is low on memory, or the array contains thousands of strings. If there is an array of std::strings then there isn't much point to using a vector to hold the same strings.
Last edited by Ancient Dragon; Oct 14th, 2009 at 11:28 pm.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,773
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 308
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
1
  #13
Oct 15th, 2009
Originally Posted by Ancient Dragon View Post
The problem with Dave's suggestion is all those strings are now stored in memory twice -- once in the array of std::strings and again in the vector of strings. That could be a problem if the computer is low on memory, or the array contains thousands of strings. If there is an array of std::strings then there isn't much point to using a vector to hold the same strings.
And this differs from using push_back with some "default" strings how?

My "Ballpark?" example was how I understood the question neithan was asking:
Originally Posted by neithan View Post
I want a vector<string> with some values by default. I wanted to do something like char **strings = {"bla", "blo"}

How to do it with vector<> without using repeated push_backs?
“The essential notion of a socialist society is force.”
— Milton Friedman
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,583
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1614
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning
 
-7
  #14
Oct 15th, 2009
Originally Posted by Dave Sinkula View Post
And this differs from using push_back with some "default" strings how?
It doesn't -- unless the strings are read from a file, in which case your example wouldn't work either
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,773
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 308
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
0
  #15
Oct 15th, 2009
Originally Posted by Ancient Dragon View Post
It doesn't -- unless the strings are read from a file, in which case your example wouldn't work either
No. But there are other methods that don't use push_back , like using a back_inserter with copy perhaps; or maybe something about writing an insert_iterator .
“The essential notion of a socialist society is force.”
— Milton Friedman
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 65
Reputation: neithan is an unknown quantity at this point 
Solved Threads: 2
neithan's Avatar
neithan neithan is offline Offline
Junior Poster in Training
 
0
  #16
Oct 15th, 2009
Originally Posted by Ancient Dragon View Post
If there is an array of std::strings then there isn't much point to using a vector to hold the same strings.
I agree but i've seen it in a lot of example codes. I asked in this forum if i should learn to use array or vector and i got a major answer of learning vector because it's standard (shall i say modern?) although not forgetting the array.

I think people would use Dave's suggestion to take the advantadges of vector but having it initialized somehow.

I'm surprised array can be inialized directly and vector not. That's kind of a weakness isn't?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,773
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 308
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
0
  #17
Oct 17th, 2009
Originally Posted by neithan View Post
I'm surprised array can be inialized directly and vector not. That's kind of a weakness isn't?
Arrays can suffer from the same copy penalty mentioned earlier -- a copy lying around for use in the initialization. For example:
  1. int foo(int x)
  2. {
  3. int array[] = {1,2,3,4,5};
  4. return array[x];
  5. }
  6.  
  7. char *bar(int x)
  8. {
  9. char *text[] = {"one","two","three","four","five"};
  10. return text[x];
  11. }
Perhaps it's the "default values for a modifiable object" that seems to make more of an issue of it.
“The essential notion of a socialist society is force.”
— Milton Friedman
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 65
Reputation: neithan is an unknown quantity at this point 
Solved Threads: 2
neithan's Avatar
neithan neithan is offline Offline
Junior Poster in Training
 
0
  #18
Oct 18th, 2009
BTW, shouldn't:

char *bar(int x)
{
char *text[] = {"one","two","three","four","five"};
return text[x];
}

be char **bar(int x) ?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,773
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 308
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c
 
0
  #19
Oct 19th, 2009
No.
“The essential notion of a socialist society is force.”
— Milton Friedman
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1386 | Replies: 18
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC