Questions tagged [stdvector]

std::vector is a contiguous sequence container in the C++ standard library. Its storage is handled automatically, so appending elements or resizing may cause the vector to allocate more memory. Use this tag for questions about std::vector or involving a std::vector.

A std::vector is a sequential container that (most notably) supports:

  • random access to elements using operator[]
  • constant time insertion and removal of elements at the end using push_back() and pop_back()
  • linear time removal of elements at the beginning or in the middle using erase()

The storage of the vector is handled automatically, being expanded and contracted as needed. Vectors usually occupy more space than static arrays, because more memory is allocated to handle future growth. This way a vector does not need to reallocate each time an element is inserted, but only when the additional memory is exhausted.

See also the Standard Library .

2908 questions
969
votes
29 answers

Concatenating two std::vectors

How do I concatenate two std::vectors?
yigal
408
votes
33 answers

How do I print out the contents of a vector?

How do I print out the contents of a std::vector to the screen? A solution that implements the following operator<< would be nice as well: template std::ostream &…
forthewinwin
  • 4,455
  • 4
  • 19
  • 17
260
votes
7 answers

Fastest way to reset every value of std::vector to 0

What's the fastest way to reset every value of a std::vector to 0 and keeping the vectors initial size ? A for loop with the [] operator ?
Matthieu Riegler
  • 31,918
  • 20
  • 95
  • 134
231
votes
8 answers

Is std::vector copying the objects with a push_back?

After a lot of investigations with valgrind, I've made the conclusion that std::vector makes a copy of an object you want to push_back. Is that really true ? A vector cannot keep a reference or a pointer of an object without a copy ?! Thanks
benlaug
  • 2,691
  • 2
  • 17
  • 15
206
votes
4 answers

Choice between vector::resize() and vector::reserve()

I am pre-allocating some memory to my a vector data member. Example: class A { vector t_Names; public: A () : t_Names(1000) {} }; At some point in time, if the t_Names.size() equals 1000, I intend to increase the size by 100.…
iammilind
  • 68,093
  • 33
  • 169
  • 336
188
votes
10 answers

C++ valarray vs. vector

I like vectors a lot. They're nifty and fast. But I know this thing called a valarray exists. Why would I use a valarray instead of a vector? I know valarrays have some syntactic sugar, but other than that, when are they useful?
rlbond
  • 65,341
  • 56
  • 178
  • 228
180
votes
9 answers

Efficient way to return a std::vector in c++

How much data is copied, when returning a std::vector in a function and how big an optimization will it be to place the std::vector in free-store (on the heap) and return a pointer instead i.e. is: std::vector *f() { std::vector *result = new…
Morten
  • 2,148
  • 2
  • 15
  • 16
168
votes
8 answers

C++, copy set to vector

I need to copy std::set to std::vector: std::set input; input.insert(5); input.insert(6); std::vector output; std::copy(input.begin(), input.end(), output.begin()); //Error: Vector iterator not dereferencable Where is the…
CrocodileDundee
  • 1,853
  • 3
  • 13
  • 8
154
votes
7 answers

How do I sort a vector of pairs based on the second element of the pair?

If I have a vector of pairs: std::vector > vec; Is there and easy way to sort the list in increasing order based on the second element of the pair? I know I can write a little function object that will do the work, but is there…
David Norman
  • 19,396
  • 12
  • 64
  • 54
150
votes
8 answers

vector::at vs. vector::operator[]

I know that at() is slower than [] because of its boundary checking, which is also discussed in similar questions like C++ Vector at/[] operator speed or ::std::vector::at() vs operator[] << surprising results!! 5 to 10 times slower/faster!. I just…
LihO
  • 41,190
  • 11
  • 99
  • 167
134
votes
6 answers

How to shuffle a std::vector?

I am looking for a generic, reusable way to shuffle a std::vector in C++. This is how I currently do it, but I think it's not very efficient because it needs an intermediate array and it needs to know the item type (DeckCard in this…
laurent
  • 88,262
  • 77
  • 290
  • 428
105
votes
5 answers

In C++ check if std::vector contains a certain value

Is there any built in function which tells me that my vector contains a certain element or not e.g. std::vector v; v.push_back("abc"); v.push_back("xyz"); if (v.contains("abc")) // I am looking for one such feature, is there any …
Jame
  • 21,150
  • 37
  • 80
  • 107
99
votes
11 answers

Using std::vector as view on to raw memory

I'm using a external library which at some point gives me a raw pointer to an array of integers and a size. Now I'd like to use std::vector to access and modify these values in place, rather than accessing them with raw pointers. Here is an…
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
93
votes
5 answers

How to compare two vectors for equality element by element in C++?

Is there any way to compare two vectors? if (vector1 == vector2) DoSomething(); Note: Currently, these vectors are not sorted and contain integer values.
Jame
  • 21,150
  • 37
  • 80
  • 107
93
votes
2 answers

std::dynarray vs std::vector

C++14 presents std::dynarray: std::dynarray is a sequence container that encapsulates arrays with a size that is fixed at construction and does not change throughout the lifetime of the object. std::dynarray must be allocated in run-time as…
masoud
  • 55,379
  • 16
  • 141
  • 208
1
2 3
99 100