std :: unique_ptr and exception security

Do I need to wrap std::unique_ptr usage with try/catch in code which should be exception safe? std::unique_ptr will hold a raw memory block created by ::malloc (with my custom deleter to call ::free).All of std::unique_ptr's constructors* are noexcep

Sorting objects in a list and Combining values

This question already has an answer here: Sorting a list of a custom type 3 answers I have an std::list<object> and object has 6 integer and a char array elements in it. I want to sort these objects with a specific integer (lets say integer a) and a

Is it a good idea to use the pointer on stl vector?

In Stanley Lippman's book there is an example, when he uses vector <string> * var; why does he create a pointer to vector? Is it useful to create a pointer to vector sometimes? When should I do so?Pointers are not only useful for data on the heap, a

Can I disable the std :: vector copy constructor?

I'm writing code with a lot of STL vectors in it. I think I've structured it so that it's all references and move constructors, but I'd like an automated way to be sure. Is there any way to get a warning or error whenever the copy constructor gets in

unordered_map without hashing

I'd like to use a structure just like std::map but without ordering, I don't need ordering and my key is pretty huge, so it's "less than" comparision takes time. So, I saw unordered_map but it has a hash template argument, so, how to use unorder

Need help to browse an STL list

I'm currently working on a movie database project using binary search trees. Each node in the tree contains the title of the film, the year the film was made, and a linked list containing the names of all the actors. This information is all read in f

Stl list iteration error

I have to implement my own list with the stl for my data structures class here is the code I have. Playlist.h #ifndef PLAYLIST_H #define PLAYLIST_H #include <iostream> using std::ostream; #include <list> using std::list; class Song; class Play

Vector erasure giving unexpected result

Erase from vector giving different results with lambda function and function object. I'm trying to delete 3rd element from a vector of strings. With function object 3rd and 6th element is getting deleted but with lambda version the code gives expecte

Constructor std :: vector - why int and not int *?

I know that the following code can build a stl vector from an array: // the iterator constructor can also be used to construct from arrays: int myints[] = {16,2,77,29}; std::vector<int> fifth (myints, myints + sizeof(myints) / sizeof(int) ); (source

find_if on the vector and compare member variables

I have a class scene like this: class Renderer; class Scene { public: Scene(const std::string& sceneName); ~Scene(); void Render(Renderer& renderer); Camera& GetSceneCamera() const; SceneNode& GetRootNode() const; const std::string& Ge

Sort and keep track of items

Just to know, I'm talking C++ now. Suppose I have an array A = {4, 1, 5, 2, 3} and sort it in A_sorted = {1, 2, 3, 4, 5}. I would like to keep the following information: where is now element e (from array A) in the sorted array A_sorted? e.g.: elemen

Resetting the STL iterator

I try to reuse an STL iterator, but cannot find any info about this. Got problem in this code: std::vector< boost::shared_ptr<Connection> >::iterator poolbegin = pool.begin(); std::vector< boost::shared_ptr<Connection> >::iterator

Define std :: vector & lt; int & gt; to a range

What's the best way for setting an std::vector<int> to a range, e.g. all numbers between 3 and 16?You could use std::iota if you have C++11 support or are using the STL: std::vector<int> v(14); std::iota(v.begin(), v.end(), 3); or implement yo

Dynamically allocated memory management in container classes

I am having some difficulty understanding how containers are implemented in C++. Specifically, how can I deal with data allocated on the stack vs data allocated on the heap. For instance: vector<int> VectorA; VectorA.push_back (1); VectorA.push_back

How to find something in a vector

I've got a pointer to a Vector with buildings. vector<building> * building1; building1 = gamer.getBuilding(); ( building1 is a pointer to vector with all buildings that gamer has on that moment. ) Now I want to check if in that vector a building exi

Return an empty vector of strings if the key is not found

I know it is a very bad idea, so other suggestions on how to do it efficiently will be well-received. Here's the thing. I have map<string,vector<string> > , I want to search for a key and return its corresponding value (vector of strings in th

std :: string operator + () memory leak?

I'm quite worry because I wrote a little application and it seems that there is a memory leak if I believe valgrind (What I actually do): ==9321== 251 bytes in 7 blocks are definitely lost in loss record 1 of 1 ==9321== at 0x402569A: operator new(uns

How do iterator pointers work in the STL

Hey i'm a little confused on how iterators work. Specifically a const_iterator for a class in this case. I understand that they are really just pointers to specific elements of a string or whatever you're working with, but in that case: I would think

Move the item from the list to the end in STL

I have already the list pointer of CDrawObject* std::list<CDrawObject*> elements; How I can move some element to the end of list. I see STL Algorithms Reference but i don't find this operations. How i can do it?Use the list method splice() void list

C ++ problem with push_back vector

UPDATE: The following code gives me an error Graph.cpp: In function 'std::ostream& operator<<(std::ostream&, const Graph&)': Graph.cpp:43: error: passing 'const std::map >, std::less, std::allocator > > > >' as 'this' argu

STL List - How to Find a List Item by Its Object Fields

I have a list: list<Unit *> UnitCollection; containing Unit objects, which has an accessor like: bool Unit::isUnit(string uCode) { if(this->unitCode == uCode) return true; else return false; } How do I search my UnitCollection list by uCode and r

Does std :: multiset guarantee insertion order?

I have a std::multiset which stores elements of class A. I have provided my own implementation of operator< for this class. My question is if I insert two equivalent objects into this multiset is their order guaranteed? For example, first I insert a

Any built-in function to test if 4 is in [1,2,3,4] (vector)

In Ruby I can do: [1,2,3,4].include?(4) #=>True In Haskell I can do : 4 `elem` [1,2,3,4] #=> True What should I do in C++?There isn't a built-in function doing exactly that. There is std::find which comes close, but since it doesn't return a bool it

Offset operations

I saw the following posted by one of the fellow stackoverflower and it sort of dumbfounds me. Would someone explain the shifting operations in the following code snippet: std::vector<bool> a; a.push_back(true); a.push_back(false); //... for (auto it

complex search operation map

I want to do the following: Define a map between a string and any kind of object (may be a list, integer - anything). The keys to the map can be as follow (the values are, again, not important): "AAA/123" ==> 1 "AAA/" ==> 2 "