stdvector

c++11: erase using a const_iterator

核能气质少年 提交于 2019-12-19 16:47:11
问题 I believe that since C++11, the erase function of most containers (e.g. std::vector ) accepts a const_iterator as parameter: iterator erase (const_iterator position); Still my compilers (GCC 4.8 and Clang 3.2, both using GCC libstdc++) won't allow me to use such function, even when compiling with --std=c++11 . Is it a compiler/libstdc++ bug, or did I do something bad? This is a sample code: #include <vector> int main( ) { std::vector<int> v; v.push_back( 1 ); v.push_back( 2 ); v.push_back( 3

Copy std::map into std::vector of pairs

五迷三道 提交于 2019-12-19 12:28:09
问题 I'm trying to copy a map into a vector of pair, so I can then sort the vector by the second data member of the pairs. I have resolved this doing like this: void mappedWordsListSorter(){ for (auto itr = mappedWordsList.begin(); itr != mappedWordsList.end(); ++itr){ vectorWordsList.push_back(*itr); } sort(vectorWordsList.begin(), vectorWordsList.end(), [=](pair<string, int>& a, pair<string, int>& b){return a.second > b.second;}); } I need to find a way to do this without using a raw loop, using

“glibc free(): invalid next size(fast)” on vector.push_back?

落爺英雄遲暮 提交于 2019-12-19 10:18:10
问题 When I run my program it will occasionally crash and give me this error: " glibc detected /pathtoexecutable: free(): invalid next size (fast)" The backtrace leads to a member function that just calls a vector's push_back function - void Path::add(Position p) {path.push_back(p);} I have tried googling the error and the very large majority of the problems are people allocating too little memory. But how could that be happening on an std::vector<>.push_back? What can I check for? Any help is

Inserting into a std::vector at an index via the assignment operator

感情迁移 提交于 2019-12-19 08:12:51
问题 I'm new to C++ and am curious if this is the preferred way of inserting into a std::vector std::vector<Object_I_madeup> myVector; void setAt(int x, Object_I_madeup o) { myVector[x] = o; } // set the array location at x to be o. I ask because I see a lot of things about using push_back ,or the highly confusing insert() . Is this Java-like way valid? I'd much rather do that... 回答1: myVector[x] = o; It is well-defined only if x < myVector.size() . Otherwise, it invokes undefined-behavior,

When should we use reserve() of vector?

£可爱£侵袭症+ 提交于 2019-12-19 03:47:06
问题 I always use resize() because I cannot use reserve as it gives error: vector subscript out of range. As I've read info about the differences of resize() and reserve(), I saw things like reserve() sets max. number of elements could be allocated but resize() is currently what we have. In my code I know max. number of elements but reserve() doesn't give me anything useful. So, how can I make use of reserve()? 回答1: A vector has a capacity (as returned by capacity() and a size (as returned by size

OpenGL: Using VBO with std::vector

谁说胖子不能爱 提交于 2019-12-18 18:36:00
问题 I'm trying to load an object and use VBO and glDrawArrays() to render it. The problem is that a simple float pointer like float f[]={...} does not work in my case, because I passed the limit of values that this pointer can store. So my solution was to use a vector. And it's not working... Here is my code: unsigned int vbo; vector<float*> vert; ... vert.push_back(new float(i*size)); vert.push_back(new float(height*h)); vert.push_back(new float(j*size)); ... glGenBuffers(1, &vbo); glBindBuffer

Initializing std::vector with iterative function calls

我只是一个虾纸丫 提交于 2019-12-18 11:45:17
问题 In many languages, there are generators that help to initialize collections. In C++, if one wants to initialize a vector uniformly, one can write: std::vector<int> vec(10, 42); // get 10 elements, each equals 42 What if one wants to generate different values on the fly? For example, initialize it with 10 random values, or consecutive numbers from 0 to 9? This syntax would be convenient, but it does not work in C++11: int cnt = 0; std::vector<int> vec(10, [&cnt]()->int { return cnt++;}); Is

Remove first N elements from a std::vector

孤人 提交于 2019-12-18 10:52:50
问题 I can't seem to think of a reliable way (that also compacts memory) to remove the first N elements from a std::vector . How would one go about doing that? 回答1: Since you mention that you want to compact memory, it would be best to copy everything to a new vector and use the swap idiom. std::vector<decltype(myvector)::value_type>(myvector.begin()+N, myvector.end()).swap(myvector); 回答2: Use the .erase() method: // Remove the first N elements, and shift everything else down by N indices myvec

type requirements for std::vector<type>

拜拜、爱过 提交于 2019-12-18 09:06:40
问题 I am still confused about the requirements for a type to be used with a std::vector in C++11, but this may be caused by a buggy compiler (gcc 4.7.0). This code: struct A { A() : X(0) { std::cerr<<" A::A(); this="<<this<<'\n'; } int X; }; int main() { std::vector<A> a; a.resize(4); } works fine and produces the expected output, indicating that the default ctor (explicitly given) is called (and not an implicit copy ctor). However, if I add a deleted copy ctor to the class, viz struct A { A() :

std::vector<std::string> to char* array

吃可爱长大的小学妹 提交于 2019-12-17 22:35:08
问题 I have a std::vector<std::string> that I need to use for a C function's argument that reads char* foo . I have seen how to convert a std::string to char* . As a newcomer to C++ , I'm trying to piece together how to perform this conversion on each element of the vector and produce the char* array. I've seen several closely related SO questions, but most appear to illustrate ways to go the other direction and create std::vector<std::string> . 回答1: You can use std::transform as: std::transform