stl

How to convert a string of hex values to a string?

Say I have a string like: string hex = "48656c6c6f"; Where every two characters correspond to the hex representation of their ASCII, value, eg: 0x48 0x65 0x6c 0x6c 0x6f = "Hello" So how can I get "hello" from "48656c6c6f" without having to create a lookup ASCII table? atoi() obviously won't work here. ...

Using equal(),find() on a vector<complex <double> >

Hi all, This is a pretty straightforward thing, but I've been bashing my head trying to understand. I'm trying to compare the elements of a vector<complex <double> > vec with a complex <double> num to check if num already exists on vec. If it does, it is not added. I tried to use the equal() and algorithm, with no success. Does anybody ...

Erase-remove idiom with std::set failing with constness-related error

Can someone help me out here? Compiling this code: void test() { std::set<int> test; test.insert(42); test.erase(std::remove(test.begin(), test.end(), 30), test.end()); // <- Line 33 } Is generating the following error when compiling: $ make g++ -c -Wall -pedantic-errors -Wextra -Wunused -Werror a_star.cpp /usr/lib/gcc/i686-p...

c++ string: is there good way to replace a char inside a string

I want to replace all the occurances of ' in a string to ^, but i saw string.replace is not the right function for me, do I need to write my own? It's boring. ...

C++: Loop over two vectors, remove elements of 1

Hi there, I have the following toy code, intended to remove duplicates from a vector: void overlap_removal(vector<int> &vec1, vector<int> &vec2) { for (vector<int>::iterator it1 = vec1.begin(); it1 != vec1.end(); ++it1) { for (vector<int>::iterator it2 = vec2.begin(); it2 != vec2.end(); ++it2) { if ((*it1)*(*it2) < 10) { ...

Will this memory be freed properly ?

I have a pair pointer let us suppose std::pair< A*, B* >* pointerpair. I allocated it memory and after using the pair i call delete pointerpair. Will it also call delete A and delete B and will be freeing the memory completely ? if i only call delete A and delete B but no delete pointerpair then is it a memory leak ? ...

private class data

should you keep all the data except functions in your class in private section? for example: I have a std::list of integers which i need to access in other class. how would you iterate it and would you really want to keep it private? Edit: I'm looking for an individual access to each element in other class. ...

How is end() implemented in STL containers?

So when we need to traverse a container from start to end we write something like for (i = v->begin(); i != v->end(); i++) assuming i is an iterator for container v. My question is "what guarantees that end will always point to one past the last element in container?" How does STL ensures this behavior and is there any chance that thi...

How to insert a pair of std::pair inside another std::pair?

I'm declaring a map of string to a pair of pairs as follow: std::map<std::wstring, std::pair<std::pair<long, long>, std::pair<long, long>>> reference; And I initialize it as: reference.insert(L"First", std::pair<std::pair<long, long>, std::pair<long, long>>(s...

Segmentation fault using FastDelegate

I've got a problem with my test code. It compiles well, but when I try to call delegate, program crashes. #include "..\libs\FastDelegate\FastDelegate.h" #include <string> #include <map> #include <iostream> typedef fastdelegate::FastDelegate1 <int, int> FuncPtr; struct Function { FuncPtr Ptr; int Param; Function() {}; Function (Fun...

Need help, I do not understand why following code is not getting compiled.

Header file is "graph.h" #ifndef _GRAPH_H_ #define _GRAPH_H_ #include <map> #include <vector> using namespace std; template <class T> class VERTEX { public: VERTEX(T inVertex): m_vertex(inVertex), m_visited(false){} ~VERTEX(){} private: T m_vertex; bool m_visited; }; template <class T> class GRAPH { public: GRAPH...

Should I use boost::ptr_vector<T> or vector<boost::shared_ptr<T> >?

I need a container of pointers. Would you recommend boost::ptr_vector<T> or std::vector<boost::shared_ptr<T> >? (Or something else?) If that's of interest, my actual data structure is relatively complicated (see here) and currently stores objects, not pointers, but i'd like to change that (using pointer containers), in order to get rid ...

How to copy a certain number of chars from a file to a vector the STL-way?

If I want to copy the contents of a file to a vector, I can do it like that: std::ifstream file("path_to_file"); std::vector<char> buffer(std::istream_iterator<char>(file), std::istream_iterator<char>()); My question is, how would I do this if I want to copy only the first n chars? Edit I could write my own ...

How to use STL unsorted key-value as pair in map

Hello, I need to use STL C++ map to store key value pairs. I need to store more than one data information in stl map. e.g Need to store DataType,Data and its behavior as(in param/outparam) all in string format. But map always use key value pair so if I store it like std::map<map<"int","50",>"behavior">. But always it sorts the...

Polymorphic STL compare function (class cmp class, class cmp int) for sorting

I'm implementing a game. I have a state tree and a set<> based priority queue that sorts the states on their costs. For this I have the < operator implemented as: struct DereferenceCompareState : public std::binary_function<State*, State*, bool> { bool operator()(const State* lhs, const State* rhs) const { verbose6("comparing ...

Reading/Writing from STL Map in multithreaded environment

Problem: I need to write a function which returns a value for a input key from a map. If function can't found the value in map it will fetch the value from database, write into map for future use and return the same. There can be multiple threads calling this function. I am thinking on this line: string GetData (const int key) { p...

Vector push_back Access Violation

This is probably a silly error, but it's driving me nuts trying to fix it. I have a struct: struct MarkerData { int pattId; unsigned short boneId; Ogre::Matrix4 transToBone; Ogre::Vector3 translation; Ogre::Quaternion orientation; MarkerData(int p_id, unsigned short b_id, Ogre::Matrix4 trans) { pattId = p_id; boneId = b_i...

std::map::erase infinite loop

I have a map of a vector of char's and a vector of strings. Every so often, if I've seen the vector of characters before, I'd like to add a string to my vector of strings. Below is my code to do that. map<vector<char>, vector<string>>::iterator myIter = mMyMap.find(vChars); if(myIter != mMyMap.end()) { vector<string> vStrings = my...

Are gcc's STL empty methods threadsafe?

If I have a deque or list that's being manipulated on different threads, can I call empty without a lock? The standard doesn't say anything about threads, so I know this won't be portable, but I'm using gcc 4.4. I'm also curious to know if this is safe on other implementations in case I ever decide to, say, switch to the intel compiler...

STL containers memory issue

I'm implementing my own graph library in linux (Fedora 10 and CentOS 5) with gcc 4.3.2 and using STL containers then I found some problems with memory. When I build my graph, I use a lot of memory enough to be view in top or another memory usage tool. I'm sure that I'm deallocating that memory (I reviewed the code again and again and I u...