stdvector

Initialize std::array with a range (pair of iterators)

烂漫一生 提交于 2019-11-26 20:24:14
问题 How can I initialize an std::array from a range (as defined by a pair of iterators)? Something like this: vector<T> v; ... // I know v has exactly N elements (e.g. I just called v.resize(N)) // Now I want a initialized with those elements array<T, N> a(???); // what to put here? I thought array would have a constructor taking a pair of iterators, so that I could do array<T, N> a(v.begin(), v.end()) , but it appears to have no constructors at all! I know I can copy the vector into the array,

May std::vector make use of small buffer optimization?

拜拜、爱过 提交于 2019-11-26 20:19:49
I was wondering with my colleague today whether std::vector can be implemented to make use of small buffer optimization. By looking into the C++11 draft, I read at 23.3.1p8 The expression a.swap(b), for containers a and b of a standard container type other than array, shall exchange the values of a and b without invoking any move, copy, or swap operations on the individual container elements. That at first seems to outlaw small buffer optimization, but under the as-if rule, we would be allowed to still do small buffer optimization for non-class types (since we cannot observe the copy being

Pointers to elements of std::vector and std::list

一笑奈何 提交于 2019-11-26 19:59:43
问题 I'm having a std::vector with elements of some class ClassA . Additionally I want to create an index using a std::map<key,ClassA*> which maps some key value to pointers to elements contained in the vector. Is there any guarantee that these pointers remain valid (and point to the same object) when elements are added at the end of the vector (not inserted ). I.e, would the following code be correct: std::vector<ClassA> storage; std::map<int, ClassA*> map; for (int i=0; i<10000; ++i) { storage

C++: Comparing two vectors

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 19:48:51
问题 Is there any way to compare two vectors? if (vector1 == vector2) DoSomething(); Note: Currently, these vectors are not sorted and contain integer values. 回答1: Check std::mismatch method of C++. comparing vectors has been discussed on DaniWeb forum and also answered . C++: Comparing two vectors Check the below SO post. will helpful for you. they have achieved the same with different-2 method. Compare two vectors C++ 回答2: Your code ( vector1 == vector2 ) is correct C++ syntax. There is an ==

Converting between C++ std::vector and C array without copying

烈酒焚心 提交于 2019-11-26 17:58:33
问题 I would like to be able to convert between std::vector and its underlying C array int* without explicitly copying the data. Does std::vector provide access to the underlying C array? I am looking for something like this vector<int> v (4,100) int* pv = v.c_array(); EDIT: Also, is it possible to do the converse, i.e. how would I initialize an std::vector from a C array without copying? int pv[4] = { 4, 4, 4, 4}; vector<int> v (pv); 回答1: You can get a pointer to the first element as follows: int

How to remove duplicates from unsorted std::vector while keeping the original ordering using algorithms?

谁都会走 提交于 2019-11-26 16:26:38
I have an array of integers that I need to remove duplicates from while maintaining the order of the first occurrence of each integer. I can see doing it like this, but imagine there is a better way that makes use of STL algorithms better? The insertion is out of my control, so I cannot check for duplicates before inserting. int unsortedRemoveDuplicates(std::vector<int> &numbers) { std::set<int> uniqueNumbers; std::vector<int>::iterator allItr = numbers.begin(); std::vector<int>::iterator unique = allItr; std::vector<int>::iterator endItr = numbers.end(); for (; allItr != endItr; ++allItr) {

Is there a standard way of moving a range into a vector?

荒凉一梦 提交于 2019-11-26 15:44:35
问题 Consider the following program which inserts a range of elements into a vector: vector<string> v1; vector<string> v2; v1.push_back("one"); v1.push_back("two"); v1.push_back("three"); v2.push_back("four"); v2.push_back("five"); v2.push_back("six"); v1.insert(v1.end(), v2.begin(), v2.end()); This efficiently copies the range, allocating enough space in the target vector for the entire range so that a maximum of one resize will be required. Now consider the following program which attempts to

Why does reallocating a vector copy instead of moving the elements? [duplicate]

耗尽温柔 提交于 2019-11-26 13:57:56
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: How to enforce move semantics when a vector grows? insert , push_back and emplace ( _back ) can cause a reallocation of a std::vector . I was baffled to see that the following code copies the elements instead of moving them while reallocating the container. #include <iostream> #include <vector> struct foo { int value; explicit foo(int value) : value(value) { std::cout << "foo(" << value << ")\n"; } foo(foo const

Vector going out of bounds without giving error

断了今生、忘了曾经 提交于 2019-11-26 12:30:00
I have a std::vector . I check its size which is 6 but when I try to access vec[6] to check whether it will give error, I get no error but some number instead. Should not it give an error? edit: something like: struct Element { std::vector<double> face; }; int main() { Element elm; .... // insert 6 elements into elm.face std::cout << elm.face.size() << std::endl; // answer is 6 std::cout << elm.face[6] << std::endl; // answer is some number } kgraney STL vectors perform bounds checking when the .at() member function is called, but do not perform any checks on the [] operator . When out of

Performance issue for vector::size() in a loop in C++

怎甘沉沦 提交于 2019-11-26 12:27:04
问题 In the following code: std::vector<int> var; for (int i = 0; i < var.size(); i++); Is the size() member function called for each loop iteration, or only once? 回答1: In theory , it is called each time, since a for loop: for(initialization; condition; increment) body; is expanded to something like { initialization; while(condition) { body; increment; } } (notice the curly braces, because initialization is already in an inner scope) In practice , if the compiler understands that a piece of your