stl

STLifying C++ classes

I'm trying to write a class which contains several std::vectors as data members, and provides a subset of vector's interface to access them: class Mesh { public: private: std::vector<Vector3> positions; std::vector<Vector3> normals; // Several other members along the same lines }; The main thing you can do with a mesh is add pos...

C++ linked list based tree structure. Sanely copy nodes between lists.

edit Clafification: The intention is not to remove the node from the original list. But to create an identical node (data and children wise) to the original and insert that into the new list. In other words, a "move" does not imply a "remove" from the original. endedit The requirements: Each Node in the list must contain a reference...

Best and shortest books on C++/STL/C#/J2SE to prepare for job inteview/tests

I am a software developer with 10+ years commercial experience, I am comfortable with nearly all of imperative languages. But I realized that most of employers prefer not candidates who is able to deliver good software but those who is trained to answer questions like "what are ten differences between pointers and references in C++" or "...

Stack STL with 2 params

Im implementing a B-tree in C++,I have a stack which saves pairs . my problem is, how i put in this stack because push only accept 1 argument. thanks ...

Confused about std::runtime_error vs. std::logic_error

I recently saw that the boost program_options library throws a logic_error if the command-line input was un-parsable. That challenged my assumptions about logic_error vs. runtime_error. I assumed that logic errors (logic_error and its derived classes) were problems that resulted from internal failures to adhere to program invariants,...

C++ STL: Trouble with string iterators

I'm making a simple command line Hangman game. void Hangman::printStatus() { cout << "Lives remaining: " << livesRemaining << endl; cout << getFormattedAnswer() << endl; } string Hangman::getFormattedAnswer() { return getFormattedAnswerFrom(correctAnswer.begin(), correctAnswer.end()); } string Hangman::getFormattedAnswerFr...

C++: Templates for static functions?

I have a static Utils class. I want certain methods to be templated, but not the entire class. How do I do this? This fails: #pragma once #include <string> using std::string; class Utils { private: template<class InputIterator, class Predicate> static set<char> findAll_if_rec(InputIterator begin, InputIterator end, Predicate ...

Does operator precedence in C++ differ for pointers and iterators?

The code below demonstrates this difference: #include <iostream> #include <string> int main() { char s[] = "ABCD"; std::string str(s); char *p = s; while(*p) { *p++ = tolower(*p); // <-- incr after assignment } std::cout << s << std::endl; std::string::i...

c++ STL vector is not acccepting the copy constructor

I wrote a code ( c++,visual studio 2010) which is having a vector, even I though copy const is declared, but is still showing that copy const is not declared Here the code #include<iostream> #include<vector> using namespace std; class A { public: A() { cout << "Default A is acting" << endl ; } A(A &a) { cout << "Copy Construc...

C++ member template for boost ptr_vector

Hi there, I'm trying to write a container class using boost::ptr_vector. Inside the ptr_vector I would like to include different classes. I'm trying to achieve that using static templates, but so far I'm not able to do that. For example, the container class is class model { private: boost::ptr_vector<elem_type> elements; public: vo...

C++: Trouble with dependent types in templates

I'm having trouble with templates and dependent types: namespace Utils { void PrintLine(const string& line, int tabLevel = 0); string getTabs(int tabLevel); template<class result_t, class Predicate> set<result_t> findAll_if(typename set<result_t>::iterator begin, set<result_t>::iterator end, Predicate pred) // warning C...

C++: Trouble with templates (C2064)

I'm having compiler errors, and I'm not sure why. What am I doing wrong here: Hangman.cpp: set<char> Hangman::incorrectGuesses() { // Hangman line 103 return Utils::findAll_if<char>(guesses.begin(), guesses.end(), &Hangman::isIncorrectGuess); } bool Hangman::isIncorrectGuess(char c) { return correctAnswer.find(c) == strin...

STL map containing references does not compile

The following: std::map<int, ClassA &> test; gives: error C2101: '&' on constant While the following std::map<ClassA &, int> test; gives error C2528: '_First' : pointer to reference is illegal The latter seems like map cannot contain a reference for the key value, since it needs to instantiate the class sometimes and a refere...

std::map insert segmentation fault

Why does this code stop with segmentation fault : class MapFile { public: /* ... */ std::map <unsigned int, unsigned int> inToOut; }; bool MapFile::LoadMapFile( const wxString& fileName ) { /* ... */ inToOut.insert( std::make_pair(input,output) ); } but when I put the "std::map inToOut;" just before "inToOut.insert" it...

how to specify a pointer to an overloaded function?

I want to pass an overloaded function to the std::for_each() algorithm. e.g.: class A { void f(char c); void f(int i); void scan(const std::string& s) { std::for_each(s.begin(), s.end(), f); } }; I'd expect the compiler to resolve f() by the iterator type. Apparently, it (gcc 4.1.2) doesn't do it. So, how can I specify which ...

How is the syntax for stl iterators implemented?

I've been working on writing a library in my spare time to familiarize myself more with c++ and singular value decomposition. I've been working on writing an Iterator class and I'm entirely capable of writing the functionality and I have already for my own currently MatrixIterator class. I'm guessing that it involves namespaces because: ...

Using an iterator without its container

I am mixing some C and C++ libraries and have only a single pointer available to do some work in a callback function. All I need to do is iterate through a vector. Here's a simplified, untested example: bool call_back(void* data){ done=... if (!done) cout << *data++ << endl; return done; } Note that this function is in an ext...

one line assert to test if STL container is sorted

Is there a way to write a one line condition that would return true if STL container is sorted? The container in question is std::vector I intend to use it in an assert ...

How do I create a set with std::pair thats sorted based on the ::second pair member using bind

I know I could use the following: template <typename Pair> struct ComparePairThroughSecond : public std::unary_function<Pair, bool> { bool operator ()(const Pair& p1, const Pair& p2) const { return p1.second < p2.second; } }; std::set<std::pair<int, long>, ComparePairThroughSecond> somevar; but wondered if i...

What is the nicest way to parse this in C++ ?

Hi, In my program, I have a list of "server address" in the following format: host[:port] The brackets here, indicate that the port is optional. host can be a hostname, an IPv4 or IPv6 address (possibly in "bracket-enclosed" notation). port, if present can be a numeric port number or a service string (like: "http" or "ssh"). If p...