stdvector

Is there a more efficient way to set a std::vector from a stream?

牧云@^-^@ 提交于 2019-12-01 16:28:32
问题 Presently, I set the value of a std::vector<char> from an std::ostringstream as follows: void foo(std::vector<char> &data, std::stringstream &stream) { data = std::vector<char>(stream.str().begin(), stream.str().end()); } I'm wondering if there is a more efficient way to do this with STL in C++ or whether the method I give here is considered appropriate? Would I be better off using std::stringstream instead? 回答1: Your method invokes undefined behaviour . stream.str() returns a string by-value

Passing std::vector<int> items to variadic function

不问归期 提交于 2019-12-01 16:15:38
I'm using gcc 4.6. Assume that there is a vector v of parameters I have to pass to a variadic function f(const char* format, ...). One approach of doing this is: void VectorToVarArgs(vector<int> &v) { switch(v.size()) { case 1: f("%i", v[0]); case 2: f("%i %i", v[0], v[1]); case 3: f("%i %i %i", v[0], v[1], v[2]); case 4: f("%i %i %i %i", v[0], v[1], v[2], v[3]); // etc... default: break; } } // where function f is void f(const char* format, ...) { va_list args; va_start (args, format); vprintf (format, args); va_end (args); } The problem is of course that it does not support an arbitrary

Automatically check bounds in std::vector [duplicate]

北城以北 提交于 2019-12-01 16:13:31
This question already has an answer here: Compile time triggered range check for std::vector 2 answers During active development of a class that uses std::vector , it often happens that an index is out of bounds. (See this code review question for a practical example.) When using operator[] , this results in undefined behavior. Still, the [] syntax is easy to read an more convenient than writing .at() . Therefore I'd like to write my code using the [] operator, but at the same time have bounds checks enabled. After testing the code, it should be very easy to remove the bounds checks. I'm

c++11: erase using a const_iterator

一曲冷凌霜 提交于 2019-12-01 16:12:50
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 ); std::vector<int>::const_iterator i = v.begin(); while( i != v.end() ) { i = v.erase( i ); } return 0

Bounds checking of std::vector (and other containers) in clang?

冷暖自知 提交于 2019-12-01 16:02:16
问题 In clang, is there a way to enable bounds checking for [] access to std::vectors and other STL containers, preferably when building in debug mode only? I just spent hours hunting down a subtle bug that turned out to be caused by us accessing past the end of a std::vector. It doesn't need to do anything clever when it detects the error, just trap in the debugger so that I can find out where it happened and fix it in the code. Is there a way to do this other than "create your own type that

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

你说的曾经没有我的故事 提交于 2019-12-01 15:46:07
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 the standard library instead. I have come across a lot of examples doing this by only transferring

Automatically check bounds in std::vector [duplicate]

僤鯓⒐⒋嵵緔 提交于 2019-12-01 15:08:35
问题 This question already has answers here : Compile time triggered range check for std::vector (2 answers) Closed last year . During active development of a class that uses std::vector , it often happens that an index is out of bounds. (See this code review question for a practical example.) When using operator[] , this results in undefined behavior. Still, the [] syntax is easy to read an more convenient than writing .at() . Therefore I'd like to write my code using the [] operator, but at the

Does std::vector use the assignment operator of its value type to push_back elements?

为君一笑 提交于 2019-12-01 14:18:27
问题 If so, why? Why doesn't it use the copy constructor of the value type? I get the following error: /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/vector.tcc: In member functio n `ClassWithoutAss& ClassWithoutAss::operator=(const ClassWithoutAss&)': /usr/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/bits/vector.tcc:238: instantiate d from `void std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterato r<typename _Alloc::pointer, std::vector<_Tp, _Alloc> >, const _Tp&) [with _Tp =

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

删除回忆录丶 提交于 2019-12-01 09:39:26
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 appreciated. You're probably doing an invalid write somewhere and trashing the control information kept by

Concatenating two std::vector — which method is more efficient and how/why?

笑着哭i 提交于 2019-12-01 09:26:01
Consider the following scenario: std::vector<int> A; std::vector<int> B; std::vector<int> AB; I want AB to have contents of A and then the contents of B in the same order. Approach 1: AB.reserve( A.size() + B.size() ); // preallocate memory AB.insert( AB.end(), A.begin(), A.end() ); AB.insert( AB.end(), B.begin(), B.end() ); Approach 2: std::vector<int> AB ( A.begin(), A.end() ); // calling constructor AB.insert ( AB.end(), B.begin(), B.end() ); Which one of the above methods is more efficient? Why? Is there a different method that is more efficient? I think the first one will always be faster