stl

What is the usefulness of project1st<Arg1, Arg2> in the STL?

I was browsing the SGI STL documentation and ran into project1st<Arg1, Arg2>. I understand its definition, but I am having a hard time imagining a practical usage. Have you ever used project1st or can you imagine a scenario? ...

Erasing elements from a vector

I want to clear a element from a vector using the erase method. But the problem here is that the element is not guaranteed to occur only once in the vector. It may be present multiple times and I need to clear all of them. My code is something like this: void erase(std::vector<int>& myNumbers_in, int number_in) { std::vector...

Is there a dereference_iterator in the STL?

I was wondering if there is an iterator in the STL that dereferences the object pointed before returning it. This could be very useful when manipulating containers aggregating pointers. Here's an example of what I would like to be able to do: #include <vector> #include <iterator> #include <algorithm> using namespace std; int main() { ...

Best way to in situ delete an element

I have a set of objects which I iterate through, however I may decide during the iteration that one (or more) of those objects now need to be deleted. My code goes as follows: if( ! m_Container.empty() ) { for( typedefedcontainer::iterator it = m_Container.begin(); it != m_Container.end(); ++i...

Looking for C++ STL-like vector class but using stack storage

Before I write my own I will ask all y'all. I'm looking for a C++ class that is almost exactly like a STL vector but stores data into an array on the stack. Some kind of STL allocator class would work also, but I am trying to avoid any kind of heap, even static allocated per-thread heaps (although one of those is my second choice). Th...

I think STL is causing my application triple its memory usage.

I am inputting a 200mb file in my application and due to a very strange reason the memory usage of my application is more than 600mb. I have tried vector and deque, as well as std::string and char * with no avail. I need the memory usage of my application to be almost the same as the file I am reading, any suggestions would be extremely ...

std::ifstream::open() not working

Hi all, I am developing a prototype for a game, and certain gameplay rules are to be defined in an ini file so that the game designers can tweak the game parameters without requiring help from me in addition to a re-compile. This is what I'm doing currently: std::ifstream stream; stream.open("rules.ini"); if (!stream.is_open()) { ...

initializing std::string from char* without copy

I have a situation where I need to process large (many GB's) amounts of data as such: build a large string by appending many smaller (C char*) strings trim the string convert the string into a C++ const std::string for processing (read only) repeat The data in each iteration are independent. My question is, I'd like to minimise (if ...

Does using STL increase footprint significantly?

Does using STL increase footprint significantly? Could you guys share your experience regarding this matter? What are the best practices to build a small footprint library? ...

Populate a vector<int> from integers in a char *

char *values = " 3 1 4 15"; vector<int> array; I want to populate the array with the values, 3,1,4,15 Is there a slick way to do it with the stl copy algorithm? ...

Finding the owner of an STL iterator

Is there any way that I can find the container pointed to by an iterator? Specifically, I want to be able to find the std::vector pointed to by a particular std::vector::iterator so that I can check the range, without having to actually pass references to that vector around. If (as I suspect) the answer is no, why not? edit: thanks fo...

Is std::map + std::tr1::bind + standard algorithms worthwhile?

This is a follow-up to my question from yesterday. I have Scott Meyers' warning about write-only code on my mind. I like the idea in principle of using standard algorithms to access the keys or values of a std::map, but the syntax required is a little baroque IMHO. Let's say I want to dump all the keys of a map to a vector. Given fol...

Finding gaps in sequence of numbers

I have a std::vector containing a handful of numbers, which are not in any particular order, and may or may not have gaps between the numbers - for example, I may have { 1,2,3, 6 } or { 2,8,4,6 } or { 1, 9, 5, 2 }, etc. I'd like a simple way to look at this vector and say 'give me the lowest number >= 1 which does not appear in the vect...

Looped push_back against resize() + iterator

Simple question; what's better and why? out.resize( in.size() ); T1::iterator outit = out.begin(); for( inIt = in.begin() to end, ++inIt, ++outIt ) *outit = *inIt OR out.erase(); for( inIt = in.begin() to end, ++inIt ) out.push_back( inIt ); I'm assuming the memory assignment implicit in push_ba...

How do stl containers get deleted?

How does container object like vector in stl get destroyed even though they are created in heap? EDIT If the container holds pointers then how to destroy those pointer objects ...

How to read/store unicode with STL strings and streams

I need to modify my program to accept Unicode, which may come from any of UTF-8 and the various UTF-16 and UTF-32 encodings. I don't really know much about Unicode (though I've read Joel Spolsky's article and the Wikipedia page). Right now I'm using an std::istream and reading my input char by char, and then storing (when necessary) in...

maximal element in an array in Java (Collections.max() for integer arrays int[])

Is there anything like Collections.max which finds the maximal value in an array for regular arrays in the standard java runtime library? ...

C++ associative array with arbitrary types for values

What is the best way to have an associative array with arbitrary value types for each key in C++? Currently my plan is to create a "value" class with member variables of the types I will be expecting. For example: class Value { int iValue; Value(int v) { iValue = v; } std::string sValue; Value(std::string v) { sValue ...

How could i create a list in c++?

How can I create a list in C++? I need it to create a linked list. How would I go about doing that? Are there good tutorials or examples I could follow? ...

std::wstring VS std::string

I am not able to understand the differences between std::string and std::wstring. I know wstring supports wide characters such as Unicode characters. I have got the following questions: When should I use std::wstring over std::string? Can std::string hold the entire ASCII character set, including the special characters? Is std::wstring...