FAQ Database Discussion Community


Parse into a vector> with boost::spirit

c++,boost,stdvector,boost-spirit
My intent is to parse a comma separated list of values into a nested vector. This list is two-dimensional. The basic question is: is it possible to parse into a vector of vector with boost::spirit? Something like the table under "Traction": ' ' RPM 0,5000,10000,15000,20000,25000 ' ' Temp ' -40.,0.,20.,40....

Segmentation fault when initializing a std::vector

c++11,segmentation-fault,stdvector
What are the possible causes of a segmentation fault at the following line? std::vector<double> d(8); ...

C++ vector remove_if pairs

c++,data-structures,stdvector
Im trying to remove pairs from vector with remove_if, but im getting errors bool MyClass::isSingleTag(const pair<int,string> & val) { string tag = val.second; int posImg, posBr; posImg = tag.find("<img"); posBr = tag.find("<br"); if (posImg == -1 && posBr == -1) { return false; } else { return true; } }...

How to find out under which key of a map a given value is stored?

c++,string,c++11,stdvector,stdmap
std::map<std::string, std::vector<std::string>> myMap How can we find out under which key of the myMap the string '55' is stored? std::vector<string> vec1 = {"11", "22", "33"}; std::vector<string> vec2 = {"44", "55"}; std::vector<string> vec3 = {}; std::string key1 = "1"; std::string key2 = "2"; std::string key3 = "3"; myMap.insert(std::make_pair(key1, vec1)); myMap.insert(std::make_pair(key2, vec2));...

how to efficiently read a binary file into a vector C++

c++,file,binary,stdvector
I need to read a large binary file (~1GB) into a std::vector<double>. I'm currently using infile.read to copy the whole thing into a char * buffer (shown below) and I currently plan to convert the whole thing into doubles with reinterpret_cast. surely there must be a way to just put...

vector::clear in libc++ for trivially destructible types

c++,c++11,clang,llvm,stdvector
Would vector<T, std::allocator<T>>::clear() be O(1) if T is trivially destructible? gcc's implementation in bits/stl_vector.h calls std::_Destroy(bits/stl_construct.h). This implementation which optimizes for the case of T being trivially destructible through tag-dispatching on std::is_trivially_destructible<T>. Looking through llvm's(3.5.0) implementation, vector::clear calls std::allocator<T>::destroy on every element, which in turn invokes the destructor. _LIBCPP_INLINE_VISIBILITY void...

vector, skewed data, when passing it to a function as pointer

c++,stdvector
I am having an interface that takes an integer pointer (I can't change this interface). I want to fill a vector in some other class and want to call that interface from the client code through my class. But when I retrieve values from vector, it always start from 0....

C++ getline segmentation fault, std::vector

c++,ifstream,stdvector,getline
I've been looking at this for a while in the debugger while Googling around, but I think I've stumbled upon some C++ behavior that I'm not familiar with. I'm going to give a quick outline on what I'm doing and what/where the problem is. I'll put code block below. The...

C++11 vector with two GLfloats as pair fail to uniform initialize

c++,c++11,stdvector,initializer-list,std-pair
The error message of gcc 4.9.2 is: could not convert from '<brace-enclosed initializer list>' to 'std::vector<std::pair<float, float> >' of this code: vector<pair<GLfloat, GLfloat>> LightOneColorsPairVec {{0.5f, 0.5f, 0.5f, 1.0f}, {0.0f, 0.0f, 1.0f, 1.0f}}; The code is compiled with 'std=c++11' compiler flag....

Changing values in polymorphic boost::shared_ptr (inside std::vector)

c++,c++11,boost,vector,stdvector
I have the following base calibration struct: struct Standard { public: unsigned long ulCamID; std::string sCalibrationModel; float fC; float fXh; float fYh; Standard() { ulCamID = 0; fC = fXh = fYh = 0.0; } virtual ~Standard() { } }; And derived structs such as: struct Aus: public Standard {...

Does std::vector::pop_back set the pointers of the objects in it to nullptr?

c++,stdvector,nullptr
Does std::vector::pop_back set the pointers of the objects in it to nullptr or does it just delete the objects? I see that the size of my vector decreases so the object is obviously deleted but I want to know whether the pointers are set to nullptr or do I have...

Setting an old vector equal to a new vector

c++,vector,data-structures,stl,stdvector
Say I'm making a class: #import <vector> class Example { std::vector<float> v; public: Example(std::vector<float>); }; How do I set the existing vector v equal to the vector passed through the constructor? Is there some "accepted" way of doing that? I've thought about looping through, and just adding them.. but it...

passing by reference in C++ 11 threads; changes are localized

c++,multithreading,c++11,stdvector,stdthread
I am trying to fasten some processing of my opencv code using C++ 11 threads. In a meanwhile I am using vector for sharing data Please consider a following piece of code: start_t = cv::getTickCount(); std::vector <std::thread> tMSE_Calc(nComparisons); std::vector<float> sum; sum.resize(nComparisons); for(int i = 0 ; i < nComparisons ;...

Realloc and glocal new/delete operator overriding

c++,c,stdvector,realloc
Stated that there is no C++ equivalent of the C realloc function, I've found in another question that such a thing is automatically managed by std::vector and we should use it instead. I'm fine with it. I guess that, since there is no other way of do reallocation, the std::vector...

Move element in array

c++,arrays,std,stdvector
I have a 2d array of vectors of pointers to Entities in a game. The array represents a grid of game world to improve performance when detecting collisions. When an entity moves I check if it moves over to another grid-tile (of course) but this is where everything goes to...

delete a pointer in std::vector exposed by boost::python

python,c++,boost,stdvector,boost-python
I have these two classes: typedef std::vector<Entity *> EntityPtrVector; class A { private: EntityPtrVector entity_vector; public: void AddEntity(Entity *); void RemoveEntity(std::string); }; class Entity { private: std::string name_; public: Entity(); Entity(std::string); std::string GetName(void) const { return name_; } void SetName(const std::string& name) { name_ = name; } }; I expose...

implementing addition operator for std::vector with type checking

c++,overloading,operator-keyword,stdvector
This is my implementation of + operator for std::vector<T>. //+ operator overloading template <typename T> std::vector<T> operator+(std::vector<T> v1, const std::vector<T>& v2) { if(v1.size() != v2.size()) { throw std::exception("Two vector size must be same"); } for(unsigned int i = 0; i<v1.size(); i++) { v1[i] += v2[i]; } return v1; } it...

Using std::lower_bound with std::vector::const_iterator

c++,c++11,stdvector
I'm trying to find bounds in the part of vector from next iterator position to the end of the vector. The code is: #include <algorithm> #include <vector> #include <iostream> int main() { typedef std::vector<int> Vector; Vector v; v.push_back(3); v.push_back(7); v.push_back(15); v.push_back(21); std::sort(v.begin(), v.end()); for (Vector::const_iterator i = v.begin(); i !=...

Which vector and map, uses less memory (large set of data and unknown size)

c++,stdvector,large-data,stdmap
I wonder which container uses less memory between std::map and std::vector with a large set of data. Loads of posts talk about efficiency, and my priority is not efficiency but memory consumption. So, if we don't know the number of our data (in my case can be over 12,000,000 entries,...

Allocating std vector content with dynamic objects

c++,malloc,std,stdvector
I have a std::vector< MyObject* > my_vector; Which is not empty and i want to allocate and push a certain amount of MyObject* in it. I know the number of objects to push. Is there a better way (optimized/faster) to do it than: int object_count = 10000; for(int index =...

How to declare std::vector with an 'n' dimensional array?

c++,c++11,vector,multidimensional-array,stdvector
My thoughts are the following: for e.g. a two-dimensional array: int a[9][9]; std::vector<int** a> b; But what if I have /* I know, it is usually a bad practise to have more than 2 dimensional arrays, but it can happen, when you need it */ int a[3][4][5][6]; std::vector<int**** a> b;...

erase-remove idiom: did I just delete something?

c++,std,stdvector
I am using the erase-remove idiom: template <typename T> bool garbageCollectVector(std::vector<T>& v) { // Use the erase-remove idiom in combination with a lambda expression v.erase( std::remove_if(v.begin(), v.end(), [this](const T& elem) -> bool { return this->shouldRemove(elem); } ), v.end()); return /* what to return? */; } and want to return whether...

C++ I/O Multiplexed server closes connections prematurely

c++,sockets,select,stdvector,berkeley-sockets
i wrote a basic network programming set of C++ classes (there's nothing more than a tcp_socket a udp_socket and an ip_address wrap class class). I'm having some troubles with a I/O multiplexed server. Let me explain: The scenario is in a way like this: class base_socket { //code for all...

iterator over multiple std containers subsequently

c++,std,stdvector
I have the following class (which obviously does not yet work as intended): class A { private: std::vector<int> firstVector, secondVector; public: std::vector<int>::iterator begin(){ return firstVector.begin(); } std::vector<int>::iterator end(){ return secondVector.end(); } }; How can I define an iterator which will go over the two member containers subsequently, e.g. after firstVector.end()-1...

How to concatenate many std::vectors?

c++,vector,stdvector
There is already a question on how to concatenate two vectors: Concatenating Two STL Vectors. However, I found it appropriate to start a new one, as my question is a bit more specific.... I have two classes that look like this: class AClass { public: std::vector<double> getCoeffs() {return coeffs;} private:...

vector is throwing bad_alloc

c++,windows,visual-c++,stdvector,bad-alloc
I have the following code that is throwing a std::bad_alloc exception: std::vector<std::vector<double>> myVector(nlines); for(int i = 0; i < nlines; i++) { try { std::vector<double> iVector(ncolumns); myVector[i] = iVector; } catch (std::exception& e) { /* catches a bad_alloc here */ } } } This code seems to work when nlines...

non-void function template recursive over tuples and vectors

templates,c++11,stdvector,stdtuple
I've made a function that calculates the sine of a number. It returns the input type if it is std::is_floating_point. But for std::is_integral, it returns a double. template<class T , typename std::enable_if<std::is_integral<T>::value>::type* = nullptr > double mysin(const T& t) // note, function signature is unmodified { double a = t;...

C++ Iterate through a vector and delete a matching string

c++,iterator,stdvector
I am trying to code a list of strings. The user can add to the list or remove from the list as well as display the current list. The displaying list and adding to list is working fine, but I can't figure out how to remove the user's string by...

Problems erasing and adding elements to std::vector

c++,stdvector
I'm having some troubles adding and removing elements from an std::vector (population, in the example code). What I want to do is to erase an element if a condition is satisfied and copy the element if instead other conditions are satisfied. Here's the code: for( int i = 0; i...

Compilation failure associated with vector of const std::string

c++,c++11,iterator,stdvector
Please can someone explain why the following code doesn't compile using clang 3.5. The error reported is 'No viable overloaded '=' in algorithm.' std::vector<const std::string> m_messages; std::vector<const std::string>::iterator iter; ... if (iter != m_messages.end()) { m_messages.erase(iter); // compilation error } If I declare m_messages as: std::vector<std::string> m_messages; then it compiles...

Vector of comma separated token to const char**

c++,arrays,string,stdvector,boost-tokenizer
I am trying to convert a comma separated string to vector of const char*. With the following code, by expected output is ABC_ DEF HIJ but I get HIJ DEF HIJ Where am I going wrong? Code: #include <iostream> #include <boost/tokenizer.hpp> #include <vector> #include <string> using namespace std; int main()...

std::list implementation & pointer arithemetic.

c++,stdvector,allocation,doubly-linked-list,stdlist
As I understand it, std::vector allocates/de-allocates all the memory it requires each time it's elements grows or shrinks, therefore pointer arithmetic can be used to iterate the vector elements. std::list on the other hand uses a double linked list, with each element pointing to the next and previous element. Assuming(possibly...

Char to ascii code conversion and back to char

c++,char,stdvector
I have small question: I have: std::vector<char> charArray(7); std::vector<int> intArray(7); int tempInt; char tempChar; Is it proper: for(int i=0; i<charArray.size();i++) { tempInt= static_cast<int>(charArray.at(i)); intArray.at(i)=tempInt; } // some operations with elemets of intArray // Like intArray.at(0) = intArray.at(1)^intArray.at(2); // next back conversion to chars for(int i=0;i<7;i++) { tempChar=static_cast<char>(intArray.at(i)); // Thanks to...

Array assignment only works with intermediate variable in C++

c++,arrays,gcc,variable-assignment,stdvector
I have a weird problem where an array assignment only works if I use an intermediate variable. Here's the setup of the program: struct A { int values[4]; }; std::vector<A> items; The items vector is inside a class, defined in its header file. The vector is filled with 'A' structs...