std::vector array question

Started by
8 comments, last by snk_kid 18 years, 10 months ago
Hello. In C I can do something like this:
char* words[] = { "foo", "bar", ... };
I was just wondering if there is a better way to do this with C++. I was thinking about using a vector of strings...
std::vector<string> v[] = { "foo", "bar", ... };
But this doesn't work, and I know the syntax is wrong as well. I haven't been able to find any examples on this so any help is much appreciated. PS - is using a vector of strings the right approach to this? Or is there something better for a C++ equivilent? Thank you in advance.
Advertisement
Yes, a vector of strings in this case is probably your best bet unless you have the need to pass elements into the front of the vector frequently, in which case a list would be a better option.

Your syntax is close. Try this:

std::vector&lt;std::string&gt; vector_of_strings; // - you can't initialize elements with the vector


and then to put the strings "foo" "bar" etc...

vector_of_strings.push_back("foo"); // push "foo" onto the END of the vectorvector_of_strings.push_back("bar"); 


hopefully this helps you.

More information: http://www.sgi.com/tech/stl/Vector.html
Well you can still do the same in c++ if you want, nothing wrong with that I guess...

If you are going to use a vector you want a single container for the array of strings not an array of vectors [smile]. There are some ways you could initialize a vector for example:

 std::vector<std::string> v(3); initialize an array with 3 elements


 std::vector<std::string> v(3,"foo"); //initialize 3 elements that equal foo :)


 std::vector<std::string> v; //push_back 2 elements into the vector "foo" and "bar" v.push_back("foo"); v.push_back("bar");


Thanks for the reply.

Quote:you can't initialize elements with the vector


Damn, I was afraid of that. So basically I have to loop through and add each string one by one? Ack, might be better off with the C solution in this case.
Quote:Original post by Majirok
Thanks for the reply.

Quote:you can't initialize elements with the vector


Damn, I was afraid of that. So basically I have to loop through and add each string one by one? Ack, might be better off with the C solution in this case.


It really depends on what you are doing. Using vectors will provide you with more built-in functionality (ability to use STL algorithms, iterators, etc) and are in general "safer" to use than a char* [] array (since vectors will "expand" rather than allow something to be written past their capacity).

However...if it's just something short and simple and you clearly understand pointers, then sometimes you don't need to bother with vectors. In almost all cases however, execept those small and trivial, I would recommend the use of vectors (or the appropriate STL container type for the functionality you require).
Quote:Original post by villiageidiot
Quote:Original post by Majirok
Thanks for the reply.

Quote:you can't initialize elements with the vector


Damn, I was afraid of that. So basically I have to loop through and add each string one by one? Ack, might be better off with the C solution in this case.


It really depends on what you are doing. Using vectors will provide you with more built-in functionality (ability to use STL algorithms, iterators, etc) and are in general "safer" to use than a char* [] array (since vectors will "expand" rather than allow something to be written past their capacity).

However...if it's just something short and simple and you clearly understand pointers, then sometimes you don't need to bother with vectors. In almost all cases however, execept those small and trivial, I would recommend the use of vectors (or the appropriate STL container type for the functionality you require).


Cheers for the advice. Basically all I'll be doing is iterating through a word list to see whether that word matches a word that the user inputs. After more Googling I came across an example on CodeGuru that seemed pretty nice and did exactly what I wanted, here's what I ended up with:

  const char* words[] = { "foo", "bar" };  size_t wordSize = sizeof(words) / sizeof(words[0]); // how many elements are in words  vector<string> vs(words, words + wordSize); // constructor takes pointer to first/one past last element    for (vector<string>::const_iterator i = vs.begin(); i != vs.end(); ++i)    cout << *i << endl; // prints foo and bar
Just FYI, the array-like initializer for a custom container class that you first mentioned is a feature that they're considering adding to the next C++ standard. So maybe we'll have it available to us someday.
"We should have a great fewer disputes in the world if words were taken for what they are, the signs of our ideas only, and not for things themselves." - John Locke
Wouldn't it make more sense to just have a file, say 'words.dat' which has all the words you need and then use file i/o to load that data into the vector of strings? Just a suggestion so you can completely bypass the array and only use the vector.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Quote:Original post by nobodynews
Wouldn't it make more sense to just have a file, say 'words.dat' which has all the words you need and then use file i/o to load that data into the vector of strings? Just a suggestion so you can completely bypass the array and only use the vector.


And that should allow for easier localization [read: translation] of your program. Granted, it's probably not a big concern, but it's a good habit to get into.
Quote:Original post by Telastyn
Quote:Original post by nobodynews
Wouldn't it make more sense to just have a file, say 'words.dat' which has all the words you need and then use file i/o to load that data into the vector of strings? Just a suggestion so you can completely bypass the array and only use the vector.


And that should allow for easier localization [read: translation] of your program. Granted, it's probably not a big concern, but it's a good habit to get into.


Okay lets do that then [grin]:

words.dat:
foo bar foobar frankgeorge jeffrey


main.cpp
#include <algorithm>  // copy#include <iterator>   // i/ostream_iterator#include <vector>     // vector#include <string>     // basic_string#include <fstream>    // basic_ifstream#include <iostream>   // coutint main() {   typedef std::vector<std::string> str_vec;   typedef std::istream_iterator<std::string> isstr_itr;   typedef std::ostream_iterator<std::string> osstr_itr;   std::ifstream in("words.dat");   str_vec v((isstr_itr(in)), isstr_itr());   std::copy(v.begin(), v.end(), osstr_itr(std::cout, ", "));}

This topic is closed to new replies.

Advertisement