All Questions

Tagged with
Filter by
Sorted by
Tagged with
1182 votes
7 answers
688k views

push_back vs emplace_back

I'm a bit confused regarding the difference between push_back and emplace_back. void emplace_back(Type&& _Val); void push_back(const Type& _Val); void push_back(Type&& _Val); As ...
ronag's user avatar
  • 50.5k
1005 votes
29 answers
687k views

Concatenating two std::vectors

How do I concatenate two std::vectors?
user avatar
853 votes
4 answers
982k views

Appending a vector to a vector [duplicate]

Assuming I have 2 standard vectors: vector<int> a; vector<int> b; Let's also say the both have around 30 elements. How do I add the vector b to the end of vector a? The dirty way would ...
sub's user avatar
  • 8,581
725 votes
16 answers
1.3m views

How do I erase an element from std::vector<> by index?

I have a std::vector<int>, and I want to delete the nth element. How do I do that? Example: std::vector<int> vec; vec.push_back(6); vec.push_back(-17); vec.push_back(12); vec.erase(???);
dau_man's user avatar
  • 7,269
541 votes
12 answers
601k views

Initializing a static std::map<int, int> in C++

What is the right way of initializing a static map? Do we need a static function that will initialize it?
Nithin's user avatar
  • 5,431
518 votes
7 answers
120k views

What's the difference between "STL" and "C++ Standard Library"?

Someone brought this article to my attention that claims (I'm paraphrasing) the STL term is misused to refer to the entire C++ Standard Library instead of the parts that were taken from SGI STL. (...)...
Pieter's user avatar
  • 32.2k
492 votes
18 answers
869k views

Iteration over std::vector: unsigned vs signed index variable

What is the correct way of iterating over a vector in C++? Consider these two code fragments, this one works fine: for (unsigned i=0; i < polygon.size(); i++) { sum += polygon[i]; } and this ...
Yuval Adam's user avatar
  • 164k
473 votes
12 answers
542k views

How to check that an element is in a std::set?

How do you check that an element is in a set? Is there a simpler equivalent of the following code: myset.find(x) != myset.end()
fulmicoton's user avatar
  • 15.8k
451 votes
11 answers
274k views

Why can't I make a vector of references?

When I do this: std::vector<int> hello; Everything works great. However, when I make it a vector of references instead: std::vector<int &> hello; I get horrible errors like ...
Colen's user avatar
  • 13.6k
442 votes
16 answers
269k views

Why does the C++ STL not provide any "tree" containers?

Why does the C++ STL not provide any "tree" containers, and what's the best thing to use instead? I want to store a hierarchy of objects as a tree, rather than use a tree as a performance enhancement....
Roddy's user avatar
  • 67.4k
402 votes
16 answers
559k views

Best way to extract a subvector from a vector?

Suppose I have a std::vector (let's call it myVec) of size N. What's the simplest way to construct a new vector consisting of a copy of elements X through Y, where 0 <= X <= Y <= N-1? For ...
An̲̳̳drew's user avatar
  • 13.7k
401 votes
11 answers
332k views

Sorting a vector in descending order

Should I use std::sort(numbers.begin(), numbers.end(), std::greater<int>()); or std::sort(numbers.rbegin(), numbers.rend()); // note: reverse iterators to sort a vector in descending order?...
fredoverflow's user avatar
375 votes
27 answers
594k views

What's the most efficient way to erase duplicates and sort a vector?

I need to take a C++ vector with potentially a lot of elements, erase duplicates, and sort it. I currently have the below code, but it doesn't work. vec.erase( std::unique(vec.begin(), vec.end(...
Kyle Ryan's user avatar
  • 4,371
364 votes
13 answers
609k views

How to sum up elements of a C++ vector?

What are the good ways of finding the sum of all the elements in a std::vector? Suppose I have a vector std::vector<int> vector with a few elements in it. Now I want to find the sum of all the ...
Prasoon Saurav's user avatar
352 votes
25 answers
584k views

How to retrieve all keys (or values) from a std::map and put them into a vector?

This is one of the possible ways I come out: struct RetrieveKey { template <typename T> typename T::first_type operator()(T keyValuePair) const { return keyValuePair.first; ...
Owen's user avatar
  • 3,941
352 votes
4 answers
539k views

C++ Erase vector element by value rather than by position? [duplicate]

vector<int> myVector; and lets say the values in the vector are this (in this order): 5 9 2 8 0 7 If I wanted to erase the element that contains the value of "8", I think I would do this: ...
Jake Wilson's user avatar
  • 89.9k
335 votes
11 answers
585k views

Determine if map contains a value for a key? [duplicate]

What is the best way to determine if a STL map contains a value for a given key? #include <map> using namespace std; struct Bar { int i; }; int main() { map<int, Bar> m; ...
Nick Heiner's user avatar
334 votes
16 answers
593k views

Sorting a vector of custom objects

How does one go about sorting a vector containing custom (i.e. user defined) objects. Probably, standard STL algorithm sort along with a predicate (a function or a function object) which would operate ...
Ankur's user avatar
  • 11.5k
312 votes
9 answers
63k views

Is std::unique_ptr<T> required to know the full definition of T?

I have some code in a header that looks like this: #include <memory> class Thing; class MyClass { std::unique_ptr< Thing > my_thing; }; If I include this header in a cpp that does ...
Klaim's user avatar
  • 68.4k
311 votes
17 answers
391k views

vector vs. list in STL

I noticed in Effective STL that vector is the type of sequence that should be used by default. What's does it mean? It seems that ignore the efficiency vector can do anything. Could anybody ...
skydoor's user avatar
  • 25.5k
292 votes
2 answers
228k views

Why can I not push_back a unique_ptr into a vector?

What is wrong with this program? #include <memory> #include <vector> int main() { std::vector<std::unique_ptr<int>> vec; int x(1); std::unique_ptr<int> ...
user383352's user avatar
  • 5,113
284 votes
16 answers
195k views

C++ sorting and keeping track of indexes

Using C++, and hopefully the standard library, I want to sort a sequence of samples in ascending order, but I also want to remember the original indexes of the new samples. For example, I have a set, ...
Mingus's user avatar
  • 2,983
282 votes
27 answers
87k views

Why use iterators instead of array indices?

Take the following two lines of code: for (int i = 0; i < some_vector.size(); i++) { //do stuff } And this: for (some_iterator = some_vector.begin(); some_iterator != some_vector.end(); ...
Jason Baker's user avatar
277 votes
21 answers
427k views

How to convert wstring into string?

The question is how to convert wstring to string? I have next example : #include <string> #include <iostream> int main() { std::wstring ws = L"Hello"; std::string s( ws.begin(), ...
BЈовић's user avatar
  • 63.4k
274 votes
19 answers
513k views

Remove spaces from std::string in C++

What is the preferred way to remove spaces from a string in C++? I could loop through all the characters and build a new string, but is there a better way?
Steve Hanov's user avatar
  • 11.5k
256 votes
11 answers
298k views

C++ equivalent of StringBuffer/StringBuilder?

Is there a C++ Standard Template Library class that provides efficient string concatenation functionality, similar to C#'s StringBuilder or Java's StringBuffer?
An̲̳̳drew's user avatar
  • 13.7k
256 votes
8 answers
108k views

What really is a deque in STL?

I was looking at STL containers and trying to figure what they really are (i.e. the data structure used), and the deque stopped me: I thought at first that it was a double linked list, which would ...
Zonko's user avatar
  • 3,484
249 votes
22 answers
123k views

Is std::vector so much slower than plain arrays?

I've always thought it's the general wisdom that std::vector is "implemented as an array," blah blah blah. Today I went down and tested it, and it seems to be not so: Here's some test results: ...
kizzx2's user avatar
  • 19k
248 votes
8 answers
135k views

What is the purpose of std::make_pair vs the constructor of std::pair?

What is the purpose of std::make_pair? Why not just do std::pair<int, char>(0, 'a')? Is there any difference between the two methods?
user avatar
247 votes
5 answers
196k views

C++ STL Vectors: Get iterator from index?

So, I wrote a bunch of code that accesses elements in an stl vector by index[], but now I need to copy just a chunk of the vector. It looks like vector.insert(pos, first, last) is the function I want.....
mpen's user avatar
  • 278k
243 votes
13 answers
79k views

Thou shalt not inherit from std::vector

Ok, this is really difficult to confess, but I do have a strong temptation at the moment to inherit from std::vector. I need about 10 customized algorithms for vector and I want them to be directly ...
Armen Tsirunyan's user avatar
240 votes
6 answers
158k views

How do I print the elements of a C++ vector in GDB?

I want to examine the contents of a std::vector in GDB, how do I do it? Let's say it's a std::vector<int> for the sake of simplicity.
John Carter's user avatar
  • 54.6k
235 votes
6 answers
205k views

C++ Double Address Operator? (&&)

I'm reading STL source code and I have no idea what && address operator is supposed to do. Here is a code example from stl_vector.h: vector& operator=(vector&& __x) // <-- Note ...
Anarki's user avatar
  • 5,093
233 votes
5 answers
155k views

When vectors are allocated, do they use memory on the heap or the stack?

Are all of the following statements true? vector<Type> vect; //allocates vect on stack and each of the Type (using std::allocator) also will be on the stack vector<Type> *vect = new ...
Phelodas's user avatar
  • 4,033
233 votes
8 answers
158k views

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 ...
benlaug's user avatar
  • 2,711
231 votes
6 answers
183k views

How to assign a std::vector using a C-style array?

What is the cheapest way to initialize a std::vector from a C-style array? Example: In the following class, I have a vector, but due to outside restrictions, the data will be passed in as C-style ...
Frank's user avatar
  • 65.3k
230 votes
6 answers
114k views

Why is std::map implemented as a red-black tree?

Why is std::map implemented as a red-black tree? There are several balanced binary search trees (BSTs) out there. What were design trade-offs in choosing a red-black tree?
Denis Gorodetskiy's user avatar
229 votes
13 answers
34k views

Why is the STL so heavily based on templates instead of inheritance?

I mean, aside from its name the Standard Template Library (which evolved into the C++ standard library). C++ initially introduce OOP concepts into C. That is: you could tell what a specific entity ...
OB OB's user avatar
  • 3,309
228 votes
6 answers
50k views

Why is it wrong to use std::auto_ptr<> with standard containers?

Why is it wrong to use std::auto_ptr<> with standard containers?
Uhall's user avatar
  • 5,743
224 votes
7 answers
33k views

Why use non-member begin and end functions in C++11?

Every standard container has a begin and end method for returning iterators for that container. However, C++11 has apparently introduced free functions called std::begin and std::end which call the ...
Jonathan M Davis's user avatar
223 votes
5 answers
262k views

How do I reverse a C++ vector?

Is there a built-in vector function in C++ to reverse a vector in place? Or do you just have to do it manually?
Dollarslice's user avatar
  • 10.1k
222 votes
7 answers
363k views

maximum value of int

Is there any code to find the maximum value of integer (accordingly to the compiler) in C/C++ like Integer.MaxValue function in java?
d3vdpro's user avatar
  • 2,937
218 votes
7 answers
24k views

Why are Standard iterator ranges [begin, end) instead of [begin, end]?

Why does the Standard define end() as one past the end, instead of at the actual end?
Puppy's user avatar
  • 146k
215 votes
13 answers
166k views

In STL maps, is it better to use map::insert than []?

A while ago, I had a discussion with a colleague about how to insert values in STL maps. I preferred map[key] = value; because it feels natural and is clear to read whereas he preferred map.insert(std:...
danio's user avatar
  • 8,625
214 votes
10 answers
83k views

In which scenario do I use a particular STL container?

I've been reading up on STL containers in my book on C++, specifically the section on the STL and its containers. Now I do understand each and every one of them have their own specific properties, and ...
Daniel Sloof's user avatar
  • 12.6k
214 votes
12 answers
252k views

How do I clear the std::queue efficiently?

I am using std::queue for implementing JobQueue class. ( Basically this class process each job in FIFO manner). In one scenario, I want to clear the queue in one shot( delete all jobs from the queue). ...
aJ.'s user avatar
  • 35.1k
213 votes
23 answers
150k views

Advantages of std::for_each over for loop

Are there any advantages of std::for_each over for loop? To me, std::for_each only seems to hinder the readability of code. Why do then some coding standards recommend its use?
missingfaktor's user avatar
210 votes
16 answers
48k views

Why is a C++ Vector called a Vector? [closed]

The question's pretty self-explanatory really. I know vaguely about vectors in maths, but I don't really see the link to C++ vectors.
Skilldrick's user avatar
  • 69.9k
209 votes
15 answers
108k views

Why would anyone use set instead of unordered_set?

C++0x is introducing unordered_set, which is available in boost and many other places. What I understand is that unordered_set is hash table with O(1) lookup complexity. On the other hand, set is ...
Khaled Alshaya's user avatar
205 votes
7 answers
123k views

Is the order of iterating through std::map known (and guaranteed by the standard)?

What I mean is - we know that the std::map's elements are sorted according to the keys. So, let's say the keys are integers. If I iterate from std::map::begin() to std::map::end() using a for, does ...
Kiril Kirov's user avatar
  • 37.8k

1
2 3 4 5
294