stl

Filtering out invalid user inputs

I'm trying to filter out invalid user inputs in a small C++ program using the following chunk of code: int selection = -1; while (!(selection >= 1 && selection <=4)) { cin >> selection; if (!(selection >= 1 && selection <=4)) { cout << "invalid selection!" << endl; cout << ...

how to execute an for loop till the queue is emptyin c++

hello, i need to execute an for loop till the queue is empty my code queue<string> q; for(int i=0;i<q.size(),i++) { // some operation goes here // some datas are added to queue } ...

Expression template operator overloading problem with std::vector.

I'm currently working on a numerical library that uses expression templates. Unfortunately I encountered a problem with my operator overloads. Consider the following stripped down example. #include <vector> namespace test { class test {}; template<class A, class B> class testExpr {}; template<class A, class B> tes...

For locale-sensitive functions, is it more common to pass the std::locale or the needed facet object(s)?

Recently I wrote a family of functions for trimming leading and trailing whitespace off of an input string. As the concept of "whitespace" is locale-dependent, I realized that I would need to either pass in a const std::ctype<char_t> reference or a const std::locale reference and call std::use_facet<std::ctype<char_t> > on the const std:...

c++ multiply vector elements by a scalar value using STL

Hi I want to (multiply,add,etc) vector by scalar value for example myv1 * 3 , I know I can do a function with a forloop , but is there a way of doing this using STL function ? something like the {Algorithm.h :: transform function } ?? ...

std::transform using C++0x lambda expression

How is this done in C++0x? std::vector<double> myv1; std::transform(myv1.begin(), myv1.end(), myv1.begin(), std::bind1st(std::multiplies<double>(),3)); Original question and solution is here. ...

Does a vector sort invalidate iterators?

std::vector<string> names; std::vector<string>::iterator start = names.begin(); std::vector<string>::iterator end = names.end(); sort (start,end); //are my start and end valid at this point? //or they do not point to front and tail resp? ...

Why is this program overloading () operator ?

Currently I am studying Standard Template Library (STL). In this program I am storing some long values in Associative Container and then sorting them according to unit's place (according to the number in unit's place). Code : #include <iostream> #include <set> #include <functional> using namespace std; class UnitLess { public: ...

Converting arrays in stl like copy

Hi everybody! It's time for another 'how do i do this in c++ without loosing my grip'-question! This time: Considering the following code taken from cplusplus.com: template<class InputIterator, class OutputIterator> OutputIterator copy ( InputIterator first, InputIterator last, OutputIterator result ) { while (first!=last) *resul...

Push String in Stack?

I am using C++ and i want to push strings in stack like push int in stacks. For Example 3."stackoverflow" 2."is" 1."Best" 0."site" at every index of stack I want to push a string. How can I do this? ...

how to use glm's operator== in stl algorithms?

Hi everybody! Last question before the weekend ... i promise ... :) Title says it all: is it possible to use the operators defined in glm::gtx::comparison in stl algorithms? Specifically i have this code: std::vector<glm::ivec3> vecA, vecB; // vectors with content bool result = std::equal(vecA.begin(), vecA.end(), vecB.begin()); ...

What is the concatenation complexity of balanced ropes?

I've looked at different papers and here is the information that I've gathered: SGI implementation and C cords neither guarantee O(1) time concatenation for long ropes nor ~log N depth for shorter ones. Different sources contradict each other. Wikipedia claims O(1) concatenation. This page says that concatenation is O(1) only when one ...

Insert with object as key fails to compile?

I'm having trouble getting something to compile. I don't understand the error thrown out by the compiler. Some code to illustrate the problem is below. #include <map> using namespace std; class Thing { public: Thing(int n):val(n) {} bool operator < (const Thing& rhs) const { return val < rhs.val; } int ...

c++ how to make two vectors one with data the other points and reads only

In C++ I have 2 STL vectors A and V. A has data and is able to change it, V only points to data but reads only and can't modify it. So if these two vectors are inside a class what will be the syntax of Variables definition assigning A reference into V get_A() and get_V() will it return a reference or a pointer? Also If I have other...

Non-BTree data structure where inserts are done in a sorted manner but i can later remove objects from random places

i want to find an object with O(logN) and also remove with O(log N) - but no go to balanced tree implementation.. any idea's for that? ...

iterator to pointer or reference - ERROR

Hi, I have this: //function definition //Point and Range are classes made of 2 ints Point barycenter_of_vector_in_range(vector<cv::Point> &points, cv::Range range); //In other place... vector<vector<Point> > tracks_; //it has some content for (vector< vector<Point> >::const_iterator track = tracks_.begin(); track != tracks_.end(); tr...

[C++] deleting while iterating

Possible Duplicates: Vector.erase(Iterator) causes bad memory access iterate vector, remove certain items as I go. Hi, I wrote this but I am get some errors when running it for (vector< vector<Point> >::iterator track = tracks_.begin(); track != tracks_.end(); track++) { if (track->empty()) { // if track is empty, r...

(C++) How to declare a stl list as extern?

I have: std::list<Particle> particles; std::list<Particle>::iterator particleit; in my main.cpp. I need to declare both of these as extern in one of my class files, but my compiler gives me some error about a missing '>' when I try the straightforward way. How would I go about fixing this? ...

C++ unique_ptr and map

...

How to fold STL container

Hi! I need analog of Haskell's foldl function to fold any STL containers. Expected signature is like following: template Iterator, FoldingFunction, Result Result foldl( Iterator begin, Iterator end, FoldingFunction f, Result initValue); Standard STL has no such function. Does Boost have any? I know it's pretty simple to im...