0

hi,

i want to have a vector of strings. i know i could declare it by

#include <iostream>
#include <string>

using namespace std;

vector<string> vec;
vec.push_back("this");
vec.push_back("is");
vec.push_back("my");
vec.push_back("array");

but thats very tedious. isnt there a more elegant way?

if i had the vector as an argument to a function, i would have to declare it first, then fill it, and then give it to the function as vec (like if i had a function fun which takes an integer as argument, and i would have to go: int i = 5; fun (i); instead of just fun(5)).

any ideas?
thanks,
richard

3
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by bubacke
Featured Replies
  • Or just this : [code] string str[5] = {"hello","ramble","people","kitty","rope"}; vector<string> vec(str,str+5); [/code] Read More

0

> isnt there a more elegant way?
Right now, Boost::assign makes it more elegant.

#include <boost/assign.hpp>
#include <iostream>
#include <string>
#include <vector>

using namespace std;
using namespace boost::assign;

int main()
{
    vector<string> vec;

    vec += "this", "is", "my", "array";

    for (vector<string>::size_type i = 0; i < vec.size(); ++i)
        cout << vec[i] << '\n';
}

The next revision of C++ has a new feature that lets classes take an initialization list like arrays and structures, but it is not finished yet.

Edited by Radical Edward: n/a

2

Or just this :

string str[5] = {"hello","ramble","people","kitty","rope"};
vector<string> vec(str,str+5);
0

thanks a lot!

Or just this :

string str[5] = {"hello","ramble","people","kitty","rope"};
vector<string> vec(str,str+5);
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.