Все вопросы: [stl]

719 вопросов

2
голосов
8ответов
2383 просмотров

How do I escape the const_iterator trap when passing a const container reference as a parameter

I generally prefer constness, but recently came across a conundrum with const iterators that shakes my const attitude annoys me about them: MyList::const_iterator find( const MyList & list, int identifier ) { // do some stuff to find identifier return retConstItor; // has to be const...

5
голосов
2ответов
1162 просмотров

A question about C++ template syntax (STL library source code)

I am reading STL source code right now. Though I understand the meat in what I am reading in stl_list.h, I want to fully understand the following snippet (mainly related to the template syntax, I think). template class _List_base { ... typedef typename _Alloc::template rebind<_List_node&...

28
голосов
9ответов
9284 просмотров

vector <unsigned char> vs string for binary data</unsigned>

Which is a better c++ container for holding and accessing binary data? std::vector&lt;unsigned char&gt; or std::string Is one more efficient than the other? Is one a more 'correct' usage?

1
голосов
2ответов
874 просмотров

C++ - Using std::count() with abstract data types?

My code is using std::count() on a list of an abstract data type that i have defined. (Sommet or Edge in english). But it doesn't work, although i've overloaded the bool operator&lt; (const Sommet &amp;left, const Sommet &amp;right) { if(left.m_id_sommet &lt; right.m_id_sommet) return tru...

6
голосов
4ответов
20445 просмотров

How do we iterate through all elements of a set while inserting new elements to it?

consider this: // set_iterator.cpp : Defines the entry point for the console application. #include "stdafx.h" #include &lt;iostream&gt; #include &lt;set&gt; using namespace std; int _tmain(int argc, _TCHAR* argv[]) { set&lt;int&gt; a1; set&lt;int&gt; a2; a1.insert(3); a1.inse...

1
голосов
1ответов
1166 просмотров

How to get combination of two array ( vector ) by STL algorithm?

I have v1 and v2 , how should I got a new v like below? v1 = {1,2} v2 = {3,4,5} v = {f(1,3) , f(1,4) , f(1,5) f(2,3) ,f(2,4) ,f(2,5)} I know I could do it using two loops, But If there is more idiomatic way like using STL algorithm? //using two loops for iter1 of v1 for iter2 of v2 ...

2
голосов
2ответов
382 просмотров

STL list_iterator code question (STL 4.0.0)

Can anyone explain why the _List_const_iterator would be using _List_node_base and downcast it to _List_node when needed? -- I think there must be some reason behind this. Thanks struct _List_node_base { _List_node_base* _M_next; ///&lt; Self-explanatory _List_node_base* _M_prev; /...

4
голосов
1ответов
1001 просмотров

Why do we need an inserter function call when doing a set_union for a set?

i need to make the call to the set_union function of STL like this: set&lt;int&gt; a1, a2; set_union(a1.begin(), a1.end(), a2.begin(), a2.end(), inserter(a1, a1.begin()); and not set_union(a1.begin(), a1.end(), a2.begin(), a2.end(), a1.begin()); why is that so?

14
голосов
2ответов
35884 просмотров

STL Priority Queue on custom class

I'm having a lot of trouble getting my priority queue to recognize which parameter it should sort by. I've overloaded the less than operator in my custom class but it doesn't seem to use it. Here's the relevant code: Node.h class Node { public: Node(...); ~Node(); bool operator&l...

1
голосов
4ответов
2360 просмотров

Using a std::map as an associative array

So, I'm using a std::map as an associative array. The map is declared as such: std::map&lt;int, CustomClass*&gt; CustomContainer; Later on, I use the CustomContainer object as an associative array, e.g., CustomClass* pClass = CustomContainer[ID] Josuttis states: If you use a key as the ...

14
голосов
6ответов
6361 просмотров

Does std::vector.pop_back() change vector's capacity?

If I allocated an std::vector to a certain size and capacity using resize() and reserve() at the beginning of my program, is it possible that pop_back() may "break" the reserved capacity and cause reallocations?

0
голосов
2ответов
3152 просмотров

C++ Read File Into hash_map

I'm trying to read in a list of words and save them in a C++ STL hash_map along with their position in the alphabetically sorted file. The idea is later I'll need to be able to tell if a string is a word and whether it comes before or after a different word. ifstream f_dict ("dictionary.txt"); ...

3
голосов
6ответов
502 просмотров

Ternary operator on auto_ptr content not working

I initialize an auto_ptr to NULL and later in the game I need to know if it has NULL or not to return it or a new copy. I've tried this auto_ptr&lt;RequestContext&gt; ret = (mReqContext.get() != 0) ? mReqContext : new RequestContext(); And several other similar stuff casting and so, but g++ t...

1
голосов
2ответов
911 просмотров

C++ templated functors

I was wondering if anyone can help me with functors. I dont really understand what functors are and how they work I have tried googling it but i still dont get it. how do functors work and how do they work with templates

-1
голосов
4ответов
201 просмотров

C++ writing a modify_if function

I have a hw question which confuses me on what i have to do. The question is below: The idea is to design a generic function called Modify_If that will take an input x (passed by reference), and two functors f1 and f2. The function Modify_If will use functor f1 to determine whether x obeys a ce...

1
голосов
3ответов
5244 просмотров

C++ using list with count() function

I have a list L which needs to count how many 1s it has in it. list&lt;int&gt; L; L.push_back(14); L.push_back(5); L.push_back(22); L.push_back(1); L.push_back(1); L.push_back(-7); the function that i have been given is : assert ( count(...,...,...) == 2); i need to know what would replac...

62
голосов
9ответов
122260 просмотров

Delete all items from a c++ std::vector

I'm trying to delete everything from a std::vector by using the following code vector.erase( vector.begin(), vector.end() ); but it doesn't work. Update: Doesn't clear destruct the elements held by the vector? I don't want that, as I'm still using the objects, I just want to empty the conta...

4
голосов
3ответов
6644 просмотров

priority_queue&lt;&gt; comparison for pointers?

So I'm using the STL priority_queue&lt;&gt; with pointers... I don't want to use value types because it will be incredibly wasteful to create a bunch of new objects just for use in the priority queue. So... I'm trying to do this: class Int { public: Int(int val) : m_val(val) {} int get...

4
голосов
2ответов
4041 просмотров

How to use hash_map with char* and do string compare?

I was using std::hash_map&lt;char*,T&gt; and somehow managed to make it work but have now discovered that the default compare function, euqal_to&lt;char*&gt; does a pointer compare rather than a string compare. I've fixed this by making my own compare type (using C's strcmp and it's about 5 LOC) ...

2
голосов
3ответов
2971 просмотров

stl::find_if with user search

I was wondering if there was a way to use the stl::find_if to search for a user inputted value I don't know to do that without using any bad conventions(globals) or adding loads of extended code. For example, if a user inputs a int x for 10, then I want to search an vector of ints iterator = ...

7
голосов
5ответов
8342 просмотров

Removing from STL std::queue without destructing the removed object?

All the documentation I can find on the STL containers (both queue and list) say that for any of the remove functions the removed object's destructor is called. This means that I can't use std::queue any time I want a queue that's simply a list of objects needing some operation performed on them...

11
голосов
5ответов
4040 просмотров

What is the optimization level ( g++ ) you use while comparing two different algorithms written in C++ ?

I have two algorithms written in C++. As far as I know, it is conventional to compile with -O0 -NDEBUG (g++) while comparing the performance of two algorithms(asymptomatically they are same). But I think the optimization level is unfair to one of them, because it uses STL in every case. The prog...

0
голосов
7ответов
1946 просмотров

convert a pointer to a reverse vector iterator in STL

I have sort(arr, arr+n, pred); How do I sort in reverse order?

4
голосов
9ответов
6488 просмотров

Nested std::maps

Supposed I have a type I'll call NamedNestedMap std::map&lt;std::string, std::map&lt;std::string, NamedNestedMap&gt; &gt; In this case each second (value) of the pair is the same kind or type as the parent object. What I can't figure out is how to declare it. This will allows a recursive alg...

19
голосов
8ответов
9285 просмотров

Renaming first and second of a map iterator

Is there any way to rename the first and second accessor functions of a map iterator. I understand they have these names because of the underlying pair which represents the key and value, but I'd like the iterators to be a little more readable. I think this might be possible using an iterator a...

3
голосов
3ответов
3271 просмотров

std::vector and its iterator as single template typename

In order to get an "easier-to-remember" interface to the index-generating function std::distance(a,b), I came up with the idea of a better distinction of it's arguments (when used against the base of a vector: vec.begin() ) by calling a templated function with the vector and its iterator, like:...

6
голосов
4ответов
2117 просмотров

Reversing strings in a vector using for_each and bind

I was wandering how it's possible to reverese strings that are contained in a vector using a single for_each command just in one "simple" line. Yea, I know it is easy with a custom functor, but I can't accept, that it can't be done using bind (at least I couldn't do it). #include &lt;vector&gt;...

1
голосов
2ответов
3381 просмотров

Memory corrupt in adding string to vector<string> loop</string>

This is on Visual Studio 2008 on a dual-core, 32 bit Vista machine. In the debug code this runs fine, but in Release mode this bombs: void getFromDB(vector&lt;string&gt;&amp; dates) { ... sql::Resultset res = stmt-&gt;executeQuery("SELECT FROM ..."); while (res-&gt;next()) { s...

0
голосов
3ответов
1053 просмотров

AV while iterating through hash_map?

_transaction is a private member variable of my class, declared as: public: typedef stdext::hash_map&lt;wchar_t*, MyClass*, ltstr&gt; transaction_hash_map; private: transaction_hash_map _transactions; During cleanup I am trying to iterate through this list and free up any objects stil...

1
голосов
4ответов
759 просмотров

display map every time it is updated sorted by value

basically, I have the map&lt;std::string, int&gt; so if i have foo 5 bar 10 jack 3 in the map, I want to display it (notice the reverse order) bar 10 foo 5 jack 3 And every time it is updated, I want iterate through all the elements, cout them, sorted by value. What is the good wa...