stl

Searching std::string between a limit

if you know the start and end positions in string from where to begin and end the search. For example - string s = StringStringString |S |t |r |i |n |g |S |t |r |i |n |g |S |t |r |i |n |g 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 How would you find "tr" in the string specifying the the position to begin search is at inde...

Memory Leaks - STL sets

I am trying to plug up all my memory leaks (which is massive). I am new to STL. I have a class library where I have 3 sets. I am also creating a lot of memory with new in the library class for adding info to the sets... Do I need to deallocate the sets? If so, how? Here is the library.h #pragma once #include <ostream> #include <m...

Most efficient way to add data to an instance

I have a class, let's say Person, which is managed by another class/module, let's say PersonPool. I have another module in my application, let's say module M, that wants to associate information with a person, in the most efficient way. I considered the following alternatives: Add a data member to Person, which is accessed by the oth...

Get last/newly added element in std::set

can you get the last or newly added element in std::set? for example say if the loop runs to collect the elements to fill in the std::set. if on the first run the set was, [0] "A" [1] "B" [2] "D" and, on second run, the set becomes [0] "A" [1] "B" [2] "C" [3] "D" How would you check if 'C' is the new element that was added? ...

Insert an element to std::set using constructor

is it possible to insert a new element to std::set like in case of std::list for example: //insert one element named "string" to sublist of mylist std::list< std::list<string> > mylist; mylist.push_back(std::list<string>(1, "string")); Now, mylist has one element of type std::string in its sub-list of type std::list. How can you do ...

Design approach, string table data, variables, stl memory usage

I have an old structure class like this: typedef vector<vector<string>> VARTYPE_T; which works as a single variable. This variable can hold from one value over a list to data like a table. Most values are long,double, string or double [3] for coordinates (x,y,z). I just convert them as needed. The variables are managed in a map like this...

How to convert for loop to STL for_each statement.

Hi, I would like to convert my for loop to STL std::for_each loop. bool CMyclass::SomeMember() { int ii; for(int i=0;i<iR20;i++) { ii=indexR[i]; ishell=static_cast<int>(R[ii]/xStep); theta=atan2(data->pPOS[ii*3+1], data->pPOS[ii*3]); al2[ishell] += massp*...

is c++ STL algorithms and containers same across platforms and performance?

After learning good amount of c++, i'm now into STL containers and algorithms template library, my major concerns are, 1) Is this library same across different platforms like MS, linux n other os? 2) will quality or efficiency of program c++ module decrease with more use of STL containers and algorithms, i think i can't customize it to...

Sort list using stl sort function

I'm trying to sort a list (part of a class) in descending containg items of a struct but it doesn't compile(error: no match for 'operator-' in '__last - __first'): sort(Result.poly.begin(), Result.poly.end(), SortDescending()); And here's SortDescending: struct SortDescending { bool operator()(const term& t1, const term& t2) ...

How to initialize std::vector from C-style array?

What is the cheapest way to initialize a std::vector from a C-style array? Example: In the following class, I have a vector, but due to outside restrictions, the data will be passed in as C-style array: class Foo { std::vector<double> w_; public: void set_data(double* w, int len){ // how to cheaply initialize the std::vector? } ...

Can set_intersection be used with hash_set in C++?

I am calculating intersection, union and differences of sets. I have a typedef of my set type: typedef set<node_type> node_set; When it is replaced with typedef hash_set<node_type> node_set; The results are different. It's a complicated program, and before I start debugging - am I doing it right? When I use functions like this: s...

Defining < for STL sort algorithm - operator overload, functor or standalone function?

I have a stl::list containing Widget class objects. They need to be sorted according to two members in the Widget class. For the sorting to work, a less-than comparator comparing two Widget objects must be defined. There seems to be a myriad of ways to do it. From what I can gather, one can either: a. Define a comparison operator overl...

Why are string and vector distinct types?

They're both resizable arrays, and std::basic_string doesn't have any specifically character-related functions like upper(). What's special about string to make it better for character data? ...

Basic question about std::vector instantiation

This looks simple but I am confused: The way I create a vector of hundred, say, ints is std::vector<int> *pVect = new std::vector<int>(100); However, looking at std::vector's documentation I see that its constructor is of the form explicit vector ( size_type n, const T& value= T(), const Allocator& = Allocator() ); So, how does th...

How can I create Min stl priority_queue?

The default stl priority queue is a Max one (Top function returns the largest element). Say, for simplicity, that it is a priority queue of int values. ...

How to provide stl like container with public const iterator and private non-const iterator?

Hello, I have a class that includes a std::list and wish to provide public begin() and end() for const_iterator and private begin() and end() for just plain iterator. However, the compiler is seeing the private version and complaining that it is private instead of using the public const version. I understand that C++ will not overload...

Bewildering SegFault involving STL sort algorithm.

Hello everybody, I am trying to recreate the program in Column 15 of programming pearls using the STL. I am trying to create a suffix array using a string and a vector of indices. I record the list of words that I read in a string called input that acts as a list of words separated by ' ' that I read from stdin at the beginning of the...

Why do programmers sometimes refer to "C++/STL" like it's a separate language?

This may seem a trivial question, but it's one that's bothered me a lot lately. Why do some programmers refer to "C++/STL" like it's a different language? The STL is part of the C++ standard library -- and therefore is part of the language, "C++". It's not a separate component, and it does not live alone in the scope of things C++. Yet s...

Different behavior of functors (copies, assignments) in VS2010 (compared with VS2005)

When moving from VS2005 to VS2010 we noticed a performance decrease, which seemed to be caused by additional copies of a functor. The following code illustrates the problem. It is essential to have a map where the value itself is a set. On both the map and the set we defined a comparison functor (which is templated in the example). #...

Does std::vector change its address? How to avoid

Since vector elements are stored contiguously, I guess it may not have the same address after some push_back's , because the initial allocated space could not suffice. I'm working on a code where I need a reference to an element in a vector, like: int main(){ vector<int> v; v.push_back(1); int *ptr = &v[0]; for(int i=2;...