How to Pass Multiple Ints into a Vector at Once

How do I pass multiple ints into a vector at once?

Try pass array to vector:

int arr[] = {2,5,8,11,14};
std::vector<int> TestVector(arr, arr+5);

You could always call std::vector::assign to assign array to vector, call std::vector::insert to add multiple arrays.

If you use C++11, you can try:

std::vector<int> v{2,5,8,11,14};

Or

 std::vector<int> v = {2,5,8,11,14};

Add multiple values to a vector

You can just make an operator:

template <class T>
std::vector<T>& operator+=(std::vector<T>& lhs, std::initializer_list<T> l)
{
lhs.insert(std::end(lhs), l);
return lhs;
}

adding multiple elements to c++ vector at once

In C++11 you can use vector's initializer-list constructor:

vector<A> list {a1, a2, a3, a4, a5};

If C++11 isn't available you can use the iterator based constructor if you create a temporary array, but it's not as clean as the C++11 solution:

A tmp_list[] = {a1, a2, a3, a4, a5};
vector<A> list(tmp_list, tmp_list + 5};

Inserting multiple values into a vector at specific positions

One way is to create another vector with capacity equal to the original size plus the number of the elements being inserted and then do an insert loop with no reallocations, O(N) complexity:

template<class T>
std::vector<T> insert_elements(std::vector<T> const& v, std::initializer_list<std::pair<std::size_t, T>> new_elements) {
std::vector<T> u;
u.reserve(v.size() + new_elements.size());
auto src = v.begin();
size_t copied = 0;
for(auto const& element : new_elements) {
auto to_copy = element.first - copied;
auto src_end = src + to_copy;
u.insert(u.end(), src, src_end);
src = src_end;
copied += to_copy;
u.push_back(element.second);
}
u.insert(u.end(), src, v.end());
return u;
}

int main() {
std::vector<int> v{1, 3, 5};
for(auto e : insert_elements(v, {{1,2}, {2,4}}))
std::cout << e << ' ';
std::cout << '\n';
}

Output:

1 2 3 4 5 

How to push_back multiple values into a vector?

Visual Studio 2012 does not support vector initialization via initializer lists. There is a lot of C++11 support missing from the standard library included with VS2012 that is supported by the VS2012 C++ compiler itself.

Sadly, as is the case for VS2012 and was the case for gcc 4.7, awesome compiler support for the new C++11 features is hampered by partial library support which seems to always lag behind the compiler.

How to return multiple values (vector and one int value) through function

How to return multiple values (vector and one int value) through function

A function can have at most one return value.

Returning more objects can be emulated by either

  • modifying one or more objects that are global or are referenced by arguments through indirection or by
  • returning an object of class type that has multiple sub objects.

You've attempted the latter approach through the use of tuple class template. The reason it doesn't work is explained in the documentation:

template< class... Types >
tuple<VTypes...> make_tuple( Types&&... args );

For each Ti in Types..., the corresponding type Vi in VTypes... is std::decay<Ti>::type unless application of std::decay results in std::reference_wrapper<X> for some type X, in which case the deduced type is X&.

As such, your invocation of make_tuple is deduced to return tuple <vector<int>, int > which is wrong because the function is supposed to return tuple <vector<int>&, int > instead. This can be fixed using std::ref so that the correct type is deduced:

std::make_tuple(std::ref(store), component_size);

Add same value multiple times to std::vector (repeat)

It really depends what you want to do.

Make a vector of length 5, filled with ones:

std::vector<int> vec(5, 1);

Grow a vector by 5 and fill it with ones:

std::vector<int> vec;
// ...
vec.insert(vec.end(), 5, 1);

Or resize it (if you know the initial size):

std::vector<int> vec(0);
vec.resize(5, 1);

You can also fill with elements using one of the many versions of fill, for example:

fill_n(back_inserter(vec), 5, 1);

and so on.... Read the library documentation, some of these functions return useful information, too.

How to insert multiple value in vector in C++?

Give your class a constructor, like this:

Something(int x_, int y_) :x(x_), y(y_) {}

Then you can just do this:

v.push_back(Something(x,y));

In C++11, you can do this, without the constructor:

v.push_back({x,y});

Is there a way to push_back multiple variables into a vector in a for loop?

With a compiler that supports the C++11 standard you can use emplace_back, like this:

vector <Book> books;
books.emplace_back("a", "Jim John", 1000);
books.emplace_back("b", "Jim John", 1001);
books.emplace_back("c", "Billy Bill", 1002);
books.emplace_back("d", "Greg Lumburge", 1003);
books.emplace_back("e", "Dallas Orange", 1004);
books.emplace_back("f", "Old McDonald", 1005);

But easier still, with C++11, is to just list all that data in a curly braces initializer:

vector <Book> books =
{
{"a", "Jim John", 1000},
{"b", "Jim John", 1001},
{"c", "Billy Bill", 1002},
{"d", "Greg Lumburge", 1003},
{"e", "Dallas Orange", 1004},
{"f", "Old McDonald", 1005}
};

And then it's easy to add a const, if appropriate.


In C++03 you can do this instead:

static Book const data[]    =
{
Book("a", "Jim John", 1000),
Book("b", "Jim John", 1001),
Book("c", "Billy Bill", 1002),
Book("d", "Greg Lumburge", 1003),
Book("e", "Dallas Orange", 1004),
Book("f", "Old McDonald", 1005)
};
int const data_size = sizeof( data )/sizeof( *data );

vector <Book> books( data, data + data_size );


Related Topics



Leave a reply



Submit