stl-algorithm

How to read arbitrary number of values using std::copy?

Hi, I'm trying to code opposite action to this: std::ostream outs; // properly initialized of course std::set<int> my_set; // ditto outs << my_set.size(); std::copy( my_set.begin(), my_set.end(), std::ostream_iterator<int>( outs ) ); it should be something like this: std::istream ins; std::set<int>::size_type size; ins >> size; s...

Using static vs. member find method on a STL set?

I am using a set because, i want to use the quick look up property of a sorted container such as a set. I am wondering if I have to use the find member method to get the benefit of a sorted container, or can I also use the static find method in the STL algorithms? My hunch is that using the static version will use a linear search inste...

Using local classes with STL algorithms

I have always wondered why you cannot use locally defined classes as predicates to STL algorithms. In the question: Approaching STL algorithms, lambda, local classes and other approaches, BubbaT mentions says that 'Since the C++ standard forbids local types to be used as arguments' Example code: int main() { int array[] = { 1, 2, ...

Why does std queue not define a swap method specialisation

I've read that all stl containers provide a specialisation of the swap algorithm so as to avoid calling the copy constructor and two assignment operations that the default method uses. However, when I thought it would be nice to use a queue in some code I was working on I noticed that (unlike vector and deque) queue doesn't provide this ...

STL vector reserve() and copy()

Greetings, I am trying to perform a copy from one vector (vec1) to another vector (vec2) using the following 2 abbreviated lines of code (full test app follows): vec2.reserve( vec1.size() ); copy(vec1.begin(), vec1.end(), vec2.begin()); While the call to vec2 sets the capacity of vector vec2, the copying of data to vec2 seems to not ...

sum of square of each elements in the vector using for_each

Hi, As the function accepted by for_each take only one parameter (the element of the vector), I have to define a static int sum = 0 somewhere so that It can be accessed after calling the for_each . I think this is awkward. Any better way to do this (still use for_each) ? #include <algorithm> #include <vector> #include <iostream> us...

Reversing strings in a vector using for_each and bind

Hi! 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 <vector> #include <string> #include <algorithm> std...

How to use std::transform with templates

I am struggling to find out why I can't get transform to work with a template class. Here's a simplified version of the template class : template<typename T> class base { public : base() : all_() {} ~base() {} public: bool add(T t) { typename vector<T>::iterator itr = lower_bound(all_.begin(), all_.end(), t); i...

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...

C++ find method is not const?

I've written a method that I'd like to declare as const, but the compiler complains. I traced through and found that this part of the method was causing the difficulty: bool ClassA::MethodA(int x) { bool y = false; if(find(myList.begin(), myList.end(), x) != myList.end()) { y = true; } return y; } There is ...

set_union with multiset containers?

What's the return of the algorithm std:set_union when one or both input containers are multisets with duplicated objects? Do dups get lost? Let's suppose for example: multiset<int> ms1; ms1.insert(1); ms1.insert(1); ms1.insert(1); ms1.insert(2); ms1.insert(3); multiset<int> ms2; ms2.insert(1); ms2.insert(1); ms2.insert(2); ms2.insert(...

How to find all matching numbers, that sums to 'N' in a given array

My goal here is to find all possible combinations that sums to a given total. For example, if the array is 2 59 3 43 5 9 8 62 10 4 and if the total is 12, then possible combinations are 2 10 3 9 8 4 5 3 4 Here is the first set of code, that I've written. Wondering the best improvements that can be done on this. int find_numbers...

Declaring and defining a function object inside a class member function

Hello to everyone, I wonder if and how it is possible to define a function object inside a classes member function to use it directly with, for example, the std::transform function. I know the example is a bit stupid, it's just to show the problem I'm confronted with. File "example.h" class Example { public: //.. constructor and...

"vector iterator not incrementable" run-time error with set_intersection

Why does this code result in a run-time error "vector iterator not incrementable"? vector<string> s1, s2; s1.push_back("joe"); s1.push_back("steve"); s1.push_back("jill"); s1.push_back("svetlana"); s2.push_back("bob"); s2.push_back("james"); s2.push_back("jill"); s2.push_back("barbara"); s2.push_back("steve"); sort(s1.begin...

boost lambda question

#include <iostream> #include <set> #include <algorithm> #include <boost/lambda/lambda.hpp> #include <boost/bind.hpp> using namespace std; using namespace boost::lambda; class Foo { public: Foo(int i, const string &s) : m_i(i) , m_s(s) {} int get_i() const { return m_i; } const string &get_s() const { return m_s; } fri...