All Questions
14,691
questions
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 ...
1005
votes
29
answers
687k
views
Concatenating two std::vectors
How do I concatenate two std::vectors?
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 ...
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(???);
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?
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.
(...)...
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 ...
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()
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
...
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....
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 ...
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?...
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(...
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 ...
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;
...
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:
...
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;
...
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 ...
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 ...
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 ...
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> ...
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, ...
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();
...
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(), ...
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?
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?
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 ...
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:
...
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?
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.....
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 ...
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.
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 ...
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 ...
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 ...
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 ...
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?
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 ...
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?
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 ...
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?
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?
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?
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:...
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 ...
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).
...
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?
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.
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 ...
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 ...