stl

const and nonconst STL Iterators

What is the difference between a ::const_iterator and an ::iterator and where would you use one over the other? ...

std::auto_ptr or boost::shared_ptr for pImpl idiom?

When using the pImpl idiom is it preferable to use a boost:shared_ptr instead of a std::auto_ptr? I'm sure I once read that the boost version is more exception friendly? class Foo { public: Foo(); private: struct impl; std::auto_ptr<impl> impl_; }; class Foo { public: Foo(); private: struct impl; boost::shared_...

What are the "string", "stream" and "stringstream" classes in C++?

I want to know what's the difference between string and stream in c++, and what's stringstream? ...

Providing an iterator for the first element of a container of pairs

I have a container filled with pairs. I want to iterate in it using the STL generic algorithms (in my case it would be inner_product, but consider it as a generic problem). The algorithm I am using expects iterators first and last. Can I provide special iterators first and last that will iterate not on the pairs but on the first element ...

STL String to lower case

Hi, I want to convert an STL String to lowercase. I am aware of the function tolower() however in the past I have had issues with this function and it is hardly ideal anyway as use with a string would require iterating through each character. Is there an alternative which works 100% of the time? ...

C++: Returning an Iterator

I have a function which searches an STL container then returns the iterator when it finds the position, however I am getting some funny error messages, can tell me what I am doing wrong? Function: std::vector< CClass >::iterator CClass::SearchFunction( const std::string& strField ) { ... return it; ... } Error: error C2664: 'st...

resize a vector down.. c++

I have a vector with 1000 "nodes" if(count + 1 > m_listItems.capacity()) m_listItems.reserve(count + 100); The problem is I also clear it out when I'm about to refill it. m_listItems.clear(); The capacity doesn't change. I've used the resize(1); but that doesn't seem to alter the capacity. So how does one change the reserv...

Adding types to the std namespace

Is it acceptable to add types to the std namespace. For example, I want a TCHAR-friendly string, so is the following acceptable? #include <string> namespace std { typedef basic_string<TCHAR> tstring; } Or should I use my own namespace? ...

Displaying dereferenced STL iterators in gdb

I have an iterator to a map element, and I would like gdb to show me the values of the "first" and "second" elements of that iterator. For example: std::map<int,double> aMap; ...fill map... std::map<int,double>::const_iterator p = aMap.begin(); I can use p.first and p.second in the code, but can't see them in gdb. For what it's worth,...

How do I examine the contents of an std::vector in gdb, using the icc compiler?

I want to examine the contents of a std::vector in gdb but I don't have access to _M_impl because I'm using icc, not gcc, how do I do it? Let's say it's a std::vector for the sake of simplicity. There is a very nice answer here but this doesn't work if I use icc, the error message is "There is no member or method named _M_impl". There a...

Writing stringstream contents into ofstream

I'm currently using a std::ofstream a function and a std::stringstream std::ofstream outFile; outFile.open(output_file); Then I call a function GetHolesResults(..., std::ofstream &outFile){ float x = 1234; std::stringstream ss; ss << x << std::endl; outFile << ss; } Now my outFile contains nothing but garbage "0012E708" re...

Developing as a programmer

Hi all, I have been learning C++ for three months now and in that time created a number of applications for my company. I consider myself fairly comfortable with C++ / MFC and STL, however I don't just want to be an OK programmer, I want to be a good programmer. I have a few books on best practices but I was wondering if anyone could su...

In STL maps, is it better to use map::insert than []?

A while ago, I had a discussion with a colleague about how to insert values in STL maps. I preferred map[key] = value; because it feels natural and is clear to read whereas he preferred map.insert(std::make_pair(key, value)) I just asked him and neither of us can remember the reason why insert is better, but I am sure it was not just a...

Single statement method to remove elements from container

Is there a single algorithm that removes elements from a container as happens in the following code? vec_it = std::remove_if( vec.begin(), vec.end(), pred ); vec.erase( vec_it, vec.end() ); ...

How to use std::sort with a vector of structures and compare function?

Thanks for a solution in C, now I would like to achieve this in C++ using std::sort and vector: typedef struct { double x; double y; double alfa; } pkt; vector< pkt > wektor; filled up using push_back(); compare function: int porownaj(const void *p_a, const void *p_b) { pkt *pkt_a = (pkt *) p_a; pkt *pkt_b = (pkt *) p_b; ...

Discarding the output of a function that needs an output iterator

Suppose there´s a template function in C++ that does some useful work but also outputs a sequence of values via an output iterator. Now suppose that that sequence of values sometimes is interesting, but at others is not useful. Is there a ready-to-use iterator class in the STL that can be instantiated and passed to the function and will ...

Matrix Template Library matrix inversion

I'm trying to inverse a matrix with version Boost boost_1_37_0 and MTL mtl4-alpha-1-r6418. I can't seem to locate the matrix inversion code. I've googled for examples and they seem to reference lu.h that seems to be missing in the above release(s). Any hints? @Matt suggested copying lu.h, but that seems to be from MTL2 rather than MTL4....

How to use SGI STL hash_map?

I am trying to use the SGI STL implementation I have downloaded from their site. I want to use a hashmap, because I have to store around 5.000.000 records, but it should be good: I need to be able to access it very quickly. I've tried stedext::hash_map, but it was very slow because I couldn't set the initial size. By the way, is it possi...

Is it safe to use STL (TR1) shared_ptr's between modules (exes and dlls)

I know that new-ing something in one module and delete-ing it in another can often cause problems in VC++. Problems with different runtimes. Mixing modules with staticly linked runtimes and/or dynamically linked versioning mismatches both can screw stuff up if I recall correctly. However, is it safe to use VC++ 2008's std::tr1::shared_p...

Remove all but the last 500,000 bytes from a file with the STL

Our logging class, when initialised, truncates the log file to 500,000 bytes. From then on, log statements are appended to the file. We do this to keep disk usage low, we're a commodity end-user product. Obviously keeping the first 500,000 bytes is not useful, so we keep the last 500,000 bytes. Our solution has some serious performanc...