FAQ Database Discussion Community


Is it safe to reuse a std container after std::move? [duplicate]

c++,c++11,std,move-semantics
This question already has an answer here: Reusing a moved container? 3 answers If I have a std::list, for example, and I use std::move() to move its information into a std::map, is it safe to reuse the list by populating it with more data so that I can add...

“Expression: list iterators incompatible” happening randomly during iteration through a list

c++,list,iterator,std
I have been trying to use std::list, however my work has been sabotaged by following error: Debug Assertion Failed! Program: C:\Windows\system32\MSVCP120D.dll File: c:\program files (x86)\microsoft visual studio 12.0\vc\include\list Line: 289 Expression: list iterators incompatible For information on how your program can cause an assertion failure, see the Visual C++ documentation...

Vectors disjunction

c++,vector,std
Which is the most elegant way to implement a logical disjunction between two std::vectors in c++? as example: vector a = {0,1,2,3,4,5,6,7,8,9}; vector b = {0,1,2,3,4}; vector c = a-b; print c = {5,6,7,8,9} ...

No matching function for call to std::transform, unresolved overloaded function type

c++,stl,compiler-errors,std
I have the following, correct and compiling code: #include <iostream> #include <map> #include <algorithm> template<typename K, typename V> void write_map(const std::multimap<K, V> mm) { std::cout << "MultiMap content:"<< std::endl << std::endl; for(auto it = mm.begin(); it != mm.end(); it++) std::cout << it->first << "\t" << it->second << std::endl; } template<typename...

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...

Split or trim string into one line

c++,string,std
I have a String which looks like this : string str = "1. Hallo \n2.Hello\n3.Bonjour\n4.Merhaba\n"; in the end I want to have sth like that: string str_line_3 = "3.Bonjour\n"; Im searching here for easy methods. To find the '\n' in a loop is to ugly imo ! Ideas ? edit:...

How do I write an int to a stringstream?

c++,std
I have std::stringstream with 1 byte: std::stringstream message; message.write((const char[]) {0x55}, 1); std::string res(message.str()); How append int a = 1; (4 bytes)? I want to get as values stored in the message's underlying std::string: 0x55 0x00 0x00 0x00 0x01 And right tool I chose for the job with the sequence...

How can I make the sign appear at the end of the fill when using std::cout and std::stringstream?

c++,std,cout,stringstream
I have the following code for formatting a printout to always be 4 digits with sign included: std::stringstream pitch; pitch.precision(0); pitch.width(4); pitch.fill('0'); pitch << std::showpos << (int)(m_values["Pitch_1"]); I would also like to show the sign ("+"/"-"), but I want it to precede the fill, as follows: +002 However, the code...

How to create iterator without type names?

c++,templates,iterator,std,c++03
For example I have a std::map<int, string> _m; and I want to iterate through it. So I have to write something like this: std::map<int, string>::iterator it = _m.begin(). The question is whether it is possible to create that iterator without using type names. Compiler knows the type of _m at...

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 =...

std::list c++ is sequential then how it can take constant time for insert and erase operations anywhere within the sequence

c++,std
in c++ reference i read "Lists are sequence containers that allow constant time insert and erase operations anywhere within the sequence, and iteration in both directions." my doubt is if it is sequential then how it can take constant time to delete and insert a node.Any way we have to...

C++: std::equal segmentation fault with my container

c++,std,c++14
I'm implementing a container like std::vector, with the aim of learning C++ and its idioms. With the purpose of overloading the operator ==, i first wrote my own version: bool operator==(const Vector& other) const{ if (_size == other._size) { for (int i = 0; i < _size; i++) if (!(_elements[i]...

Sort structs depending on non-static value (C++)

c++,sorting,std
I have an EnemyRhombus class. It is a unit, which can move to different points on the map. I want to process points, to which it can move, in order of increasing distance to them. To do it I want to sort the points. Code 1: class EnemyRhombus { public:...

String to Char Array not working

c++,arrays,string,char,std
I noticed that if my string is empty, the char array does not become empty either. See the below code: std::copy(strUsername.begin(), strUsername.end(), sRecord.m_strUsername); sRecord.m_strUsername is a char[1064], but if it is already populated and i try doing a copy (or strcpy) with strUsername as empty, nothing happens, even though I...

Remove by iterator from std::vector

c++,vector,std
For removing an iterator from std::vector I can do these two things: std::vector<int>& vec = myNumbers; // use shorter name vec.erase(std::remove(vec.begin(), vec.end(), number_in), vec.end()); Or I can do this: auto it = find(vec.begin(), vec.end(), number_in); vec.erase(it); The second is more intuitive, I guess, but which one is faster? EDIT: Elements...

atof Function not working in program

c++,string,double,std
In my c++ program I am required to make a class called ComplexNumber that holds information for a complex number. I had written all the functions in the header file and moved the functions over to the .cpp file. The atof function for my current program is not working. It...

std::vector that holds class methods

c++,vector,enums,std,std-function
I have done this to enumerate my class members enum MemberType {A, B, C, D}; class Hello { public: std::vector<std::function<void(Hello*, void)>> m_members; void func() { }; Hello() { m_members[A] = func(); } }; What is wrong here?...

How to make function like operator