Can you initialize (easily/simply) a vector with multiple values?

Like an array:int fu[] = {1, 3, 4, 5, 6}

Recommended Answers

All 3 Replies

If your compiler supports C++11 (the latest language standard), you can also initialize a vector with multiple values (called an initializer-list), like so:

std::vector<int> fu = {1,3,4,5,6};

or if your compiler doesn't support c++11, you could do someting like

int fu[] = {1, 3, 4, 5, 6};
std::vector<int> vect(fu, fu + 5);

If your compiler support new version of c++ compiler c++11, you can use the code as follows:

std::vector<int> v = {1, 2, 3, 4};

Be a part of the DaniWeb community

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