stl

more efficient sparse matrix element accessor

Hi, I wrote a small sparse matrix class with the member: std::map<int,std::map<int,double> > sm; The method below is the function i use to access the elements of a matrix, if not possible through an iterator: double matrix::operator()(int r,int c) const { std::map<int,std::map<int,double> >::const_iterator i = sm.find(r); if(...

How does std::stringstream handle wchar_t* in operator<<?

Given that the following snippet doesn't compile: std::stringstream ss; ss << std::wstring(L"abc"); I didn't think this one would, either: std::stringstream ss; ss << L"abc"; But it does (on VC++ at least). I'm guessing this is due to the following ostream::operator<< overload: ostream& operator<< (const void* val ); Does this h...

Sorting a set<string> on the basis of length

My question is related to this. I wanted to perform a sort() operation over the set with the help of a lambda expression as a predicate. My code is #include <set> #include <string> #include <iostream> #include <algorithm> int main() { using namespace std; string s = "abc"; set<string> results; do { for (int n = 1; n <= s....

Is there a good way to search by both key and value?

I am currently using map<int, int> in C++. I can check for the existence of a key without a problem but is there an efficient way to retrieve the keys that a particular value has as well? My purpose is to grab all the elements with a given value and then update their value. ...

Insert into 2D vector

Could you do the insert operation in one line along with allocating memory for internal vector? vector <vector<int>> myvector; int a[] = {0, 1, 2, 3, 4}; for (int index = 0; index < 2; index++) { myvector.push_back(vector<int>()); //allocate memory for internal vector myvector[index].insert(myvector[index].begin(),...

STL in embedded enviornment

I am a C++ programmer and over the years have been subjected to hearing the notion that STL is not good for use in embedded enviornments and hence usually prohibited in usage for embedded enviornment based projects.I believe STL libraries like Boost are far more powerful and provide a much more faster & less error prone means of developm...

parse number from the string

Say I have the line in this format "word word 12 YR" or "word word 10 MO" and I want to convert it to char * containing either "12Y" or "10M" respectively. The format is two words followed by numerical followed by the word denoting the year or the month. words are space/tab separated. Currently, I am playing around with the s...

Should I manipulate a C++ map's value via a pointer or by updating the record?

I'm using a C++ std::map to hold a large collection of entities: using std::map; map {structureEntityID, classEntityRecord} tableEntityRecords; //(replace {}s with arrows) I will be frequently modifying the entities in my table (many times a second). Is it better to modify those records via a pointer or is it better to make a local ...

Regex filter for STL messages

Given the follow STL error: ./poly_power/poly_class.cpp:496: error: no matching function for call to ‘state_operator< polynomial< variable_term< polynomial< variable_term< std::basic_string<char, std::char_traits< char>, std::allocator< char> >, int> >, polynomial< variable_term<std::basic_string< char, std::char_traits< char>, std::all...

hpp files && implememtning a class extending vector

hi, i have 2 questions. does any one know what an hpp file is? why would someone do that ? i am trying to implement a class that extends vecotr but i want to use all thee riginal functinos and add on actions for each functin. so i wrote: #include <iostream> #include <vector> #ifndef _MY_PERSONAL_VECTOR #define _MY_PERSONAL_VECTOR ...

Checking whether a vector is empty

Suppose I have a std::vector say Vector Now after performing some operations on the vector(either insertion or deletion) I want to check if the vector is empty and on the basis of that I want to perform some operations. Which approach is better Approach 1 if (Vector.size() == 0){ /* operations */ } Approach 2 if (Vector.empty()) {...

Turning remove_if into remove_not_if

How can I reverse a predicate's return value, and remove the elements that return false instead of true? Here is my code: headerList.remove_if(FindName(name)); (please ignore lack of erase) with FindName a simple functor: struct FindName { CString m_NameToFind; FindInspectionNames(const CString &nameToFind) { m...

How do you write a operator() or less-than-functor neater than a trivalue-compare-function

Writing an operator< () for a struct appears to be clearer than writing the classical trivalue compare. for example, to sort the following struct S { int val; }; you can write an operator< () bool operator< ( const S &l, const S &r ) { return l.val < r.val; } or, a trivalue function (usually in the following fashion ) i...

STL iterator into constructor

Id' like to know how to write a constructor for a custom class (a linked list in this case) that accepts any STL input iterator. I have already created a custom Iterator class which is tied to my List class. This works fine. template <typename T> List<T>::List(Iterator beg, Iterator end) : first_(0) { while (beg != end) ...

STL list erase items

I want to erase items from the list while iterating over. I have done this before, but somehow this simple example fails me. thnx for the help in advance! #include<iostream> #include<list> using namespace std; void main() { list<int> x; for ( int i =0;i<10; i++) x.push_back(i); for( list<int>::iterator k = x.begin(...

Can the 'type' of a lambda expression be expressed?

Thinking of lambda expressions as 'syntactic sugar' for callable objects, can the unnamed underlying type be expressed? An example: struct gt { bool operator() (int l, int r) { return l > r; } } ; Now, [](int l, int r) { return l > r; } is an elegant replacement for the above code (plus the necessary creation of c...

Are there any implementations of STL that are not messy and can actually be read?

Possible Duplicate: Is there a readable implementation of the STL? I've been trying to grok the source of the STL and frankly after looking at multiple implementations, I'm left shaking my head. The sheer verbosity of the code is breathtaking. No, not the complexity of the concepts, I mean the way it was implemented, the layer...

Handling STL errors without exceptions

I have a project which uses STL a lot. Now I'm working on porting the project to a specific platform which doesn't support exceptions. I can disable exceptions, however I still need to handle STL errors. Is there any approach to handle STL errors correctly with exceptions disabled? Is there any third party STL implementation which helps...

What are Containers/Adapters? C++

What are containers/adapters? Someone please explain in layman's language. I have tried to look up on the internet but the definitions and explanations are too technical and hard to understand. I have basic knowledge of C++ and its sub-topics like (class/templates/STL). EDIT 1: Can anyone please give me a practical example of the ap...

extracting last 2 words from a sequence of strings, space-separated

I have any sequence (or sentence) and i want to extract the last 2 strings. For example, sdfsdfds sdfs dfsd fgsd 3 dsfds should produce: 3 dsfds sdfsd (dfgdg)gfdg fg 6 gg should produce: 6 gg ...