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

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

Or just this :

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

thanks a lot!

Or just this :

string str[5] = {"hello","ramble","people","kitty","rope"};
vector<string> vec(str,str+5);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.