stl

c++ templated container scanner.

Hello, here's today's dilemma: suppose I've class A{ public: virtual void doit() = 0; } then various subclasses of A, all implementing their good doit method. Now suppose I want to write a function that takes two iterators (one at the beginning of a sequence, the other at the end). The sequence is a sequence of A subclasses lik...

A dynamic buffer type in C++?

I'm not exactly a C++ newbie, but I have had little serious dealings with it in the past, so my knowledge of its facilities is rather sketchy. I'm writing a quick proof-of-concept program in C++ and I need a dynamically sizeable buffer of binary data. That is, I'm going to receive data from a network socket and I don't know how much the...

Better way to count things?

In one of my programs for school, I use the following function to count the frequency of identifiers in a string, separated by newlines and #: Input: dog cat mouse # rabbit snake # Function: //assume I have the proper includes, and am using namespace std vector< pair<string,int> > getFreqcounts(string input) { vector<string> ite...

C++ stl stringstream direct buffer access.

Hello, this should be pretty common yet I find it fascinating that I couldn't find any straight forward solution. Basically I read in a file over the network into a stringstream. This is the declaration: std::stringstream membuf(std::ios::in | std::ios::out | std::ios::binary); Now I have some C library that wants direct access to th...

How to expose STL list over DLL boundary?

I have a DLL which needs to access data stored in STL containers in the host application. Because C++ has no standard ABI, and I want to support different compilers, the interface between the application and DLL basically has to remain plain-old-data. For vectors this is relatively straightforward. You can simply return the memory blo...

Find vector element in second vector

Given two vectors of integers, how to determinate if there's some element from 1st vector is present in 2nd one? ...

global vector C++

Is it possible to have a vector as a global variable is C++? Like this: class system {...}; vector<system> systems; when I try to compile this I get an error. The compiler I'm using is gcc and I'm compiling as C++. ...

Avoid making copies with vectors of vectors

I want to be able to have a vector of vectors of some type such as: vector<vector<MyStruct> > vecOfVec; I then create a vector of MyStruct, and populate it. vector<MyStruct> someStructs; // Populate it with data Then finally add someStructs to vecOfVec; vecOfVec.push_back(someStructs); What I want to do is avoid having the copy ...

c++ iterator confusion

I have a vector<list<customClass> > I have an iterator vector<list<customClass> >::const_iterator x When I try to access an member of customClass like so: x[0]->somefunc(), I get errors of a non-pointer type/not found. ...

Parsing a comma-delimited std::string

If I have a std::string containing a comma-separated list of numbers, what's the simplest way to parse out the numbers and put them in an integer array? I don't want to generalise this out into parsing anything else. Just a simple string of comma separated integer numbers such as "1,1,1,1,2,1,1,1,0". ...

What could be wrong with this?

BTW: I found the problem: (See my answer below) When I build my program at home it works fine, but when I use my universities system is crashing on me. When I go at it with GDB I get this: (gdb) r t.c- Starting program: /home/shro8822/p5/c- t.c- *--Code Gen Function: main *--in function 'main' variable offsets start at 2 Program re...

simple wildcard match with std::string

I have std::string with the follwing format std::string s = "some string with @lable" I have to find all instances of '@' and then find the identifier right after the '@' , this ID has a value (in this case 'lable' stored for it in a look up table. I will then replace the @ and the id with the found value. for example suppose t...

Common algorithm for std::list and std::map?

I have a class of interest (call it X). I have a std::list<X*> (call it L). I have a function (call it F). F(L) returns a subset of L (a std::list<X*>) according to an algorithm that examines the internal state of each X in the list. I'm adding to my application a std::map<int,X*> (call it M), and I need to define F(M) to operate in ...

Behaviour of std::partition when called on empty container?

I ran into a problem when calling std::partition on an empty container (std::list). std::list<int>::iterator end_it = std::partition(l.begin(), l.end(), SomeFunctor(42)); std::list<int>::iterator it = l.begin(); while (it != end_it) { // do stuff } If the list is empty, std::partition returns an iterator, that is not equal to l.end...

problem sorting using member function as comparator

trying to compile the following code I get this compile error, what can I do? ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. class MyClass { int * arr; // other member variables MyClass() { arr = new int[someSize]; } doCompa...

resize versus push_back in std::vector : does it avoid an unnecessary copy assignment?

Hello people. When invoking the method push_back from std::vector, its size is incremented by one, implying in the creation of a new instance, and then the parameter you pass will be copied into this recently created element, right? Example: myVector.push_back(MyVectorElement()); Well then, if I want to increase the size of the vecto...

array vs vector vs list

I am maintaining a fixed-length table of 10 entries. Each item is a structure of like 4 fields. There will be insert, update and delete operations, specified by numeric position. I am wondering which is the best data structure to use to maintain this table of information: array - insert/delete takes linear time due to shifting; update ...

Which STL container to use if I want it to ignore duplicated elements?

I am looking for some STL (but not boost) container, which after the following operations will contain 2 elements: "abc" and "xyz": std::XContainer<string> string_XContainer; string_XContainer.push_back("abc"); string_XContainer.push_back("abc"); string_XContainer.push_back("xyz"); By the way, I need it just in order to call string_XC...

Which STL containers require the use of CAdapt?

The CAdapt class is provided by Microsoft in order to enable using classes that override the address of operator (operator&) in STL containers. MSDN has this to say about the use of CAdapt: Typically, you will use CAdapt when you want to store CComBSTR, CComPtr, CComQIPtr, or _com_ptr_t objects in an STL container such as a list. O...

Why MS Visual Studio 2008 has 2 copies of <sstream> STL file ?

This and this. "c:\Program Files\Microsoft Visual Studio 9.0\VC\crt\src\sstream" "c:\Program Files\Microsoft Visual Studio 9.0\VC\include\sstream" And the files have small differences. Why 2 files ? Thank you. ...