Why is that the single parameter constructor of std::list<T> requires T to be a default-constructible type? I mean the following code does not compile.
struct Foo { // does not have default constructor.
Foo (int i) {}
}
int main(void) {
std::list<Foo> l(10);
}
It seems possible to use the construct and destroy idioms as they hav...
Regarding the C++ STL map, erasing by key:-
size_type map::erase ( const key_type& x );
Is it legal to erase a non-existing key? i.e. is the snippet below ok?
map<char,int> mymap;
mymap['c']=30;
mymap.erase('c');
mymap.erase('c');
mymap.erase('D');
Cheers
...
How can I overload the STL implementation for methods like find, erase and insert to take varying parameters? I tried to look up the overloading of STL methods but couldn't find any help.
...
I have a sorted array of double values in C++. Is there an STL function that will return the index of the nearest value in the array to a given double value?
For example, given the following array
double myarray[5] = { 1.0, 1.2, 1.4. 1.5, 1.9 };
the function call
search(myarray, 1.6);
should return 3, the index of the element nea...
Map type from STL have next type:
std::map< Key, Data, Compare, Alloc >
As one of template parameters we could pass Compare predicate, why map accept this predicate as template parameter and not as object in constructor?
It could has more flexible interface with something like boost::function< bool, const T&, const T& > in constr...
I can't use shared_ptr in my project, no boost :(
So, I'm having a class roughly similar to the one below:
class MyClass
{
private:
std::auto_ptr<MyOtherClass> obj;
};
Now, I want to store the instances of above class in std::vector. Is it safe? I've read here that it's wrong to use std::auto_ptr with STL containers. Does it apply ...
I have a function which shall return a char*. Since I have to concatenate some strings, I wrote the following line:
std::string other_text;
// ...
return ("text" + other_text).c_str();
I know that I could avoid the question naming the string I want to return. I just want to take the chance to make a more general question:
is it safe ...
The title speaks for itself ....
Does choice of container affects the speed of the default std::sort algorithm somehow or not? For example, if I use list, does the sorting algorithm just switch the node pointers or does it switch the whole data in the nodes?
...
In an answer to http://stackoverflow.com/questions/700588/is-it-safe-to-store-objects-of-a-class-which-has-an-stdautoptr-as-its-member-v I stated that a class that contained an auto_ptr could be stored in a vector provided the class had a user-defined copy constructor.
There were several comment suggesting that this was not the case, s...
Say I want to print:
============
Some message
============
And:
=======================
Other Message long one
=======================
The number of "=" changes based on the message length. What is the most efficient way to print this sort of a thing?
No boost, just STL please.
...
I've implementation of UnaryOperation like this
struct Converter
{
Converter( std::size_t value ):
value_( value ), i_( 0 )
{}
std::string operator() ( const std::string& word )
{
return ( value_ & ( 1 << i_++ ) ) ?
word:
std::string( word.size(), ' ' );
}
std::size_t value...
I have a program which is like this
list<int>:: iterator n = alist.begin();
while(n!= (list<int>::iterator)0)
{
printf("Element is %d\n",*n);
n = alist.erase(n);
}
So here i am comparing iterator with zero.
but after deleting the last element the compiler is showing this error.
*** glibc detected *** ./new: free(): invalid p...
I am using std::queue for implementing JobQueue class. ( Basically this class process each job in FIFO manner).
In one scenario, I want to clear the queue in one shot( delete all jobs from the queue).
I don't see any clear method available in std::queue class.
How do I efficiently implement the clear method for JobQueue class ?
I have ...
Hi!
I know C and C++ quite well. I know in much detail about pointers and well versed with pointer arithmetic and worked on Win32 API and a bit of MFC as well in my university days. In my previous job, I had no chance to look on these matters and worked in some other domain. Now what I want is a crash or refresher course in:
C++
STL
...
Where are the STL header files actually located in a MS Visual Studio 8 install ?
...
I was under the assumption that STL functions could be used only with STL data containers (like vector) until I saw this piece of code:
#include <functional>
#include <iostream>
#include <numeric>
using namespace std;
int main()
{
int a[] = {9, 8, 7};
cerr << "Sum: " << accumulate(&a[0], &a[3], 0, plus<int>()) << endl;
retu...
Hi folks,
I'd like to write a (C++) method that returns an std::set of custom objects. I do not however want to expose the comparator used when inserting the objects, so I make it a private class.
I create the set like this:
std::set<some_class, some_class_comparator> return_object;
now I want to return the set, so it has to be cast ...
This may seem frivolous to some of you, but which of the following 2 methods of iteration over a STL container is better? Why?
class Elem;
typedef vector<Elem> ElemVec;
ElemVec elemVec;
// Method 0
for (ElemVec::iterator i = elemVec.begin(); i != elemVec.end(); ++i)
{
Elem& e = *i;
// Do something
}
// Method 1
for (int i = 0;...
for_each accepts InputIterators :
//from c++ standard
template <class InputIterator, class Function>
Function for_each (InputIterator first, InputIterator last, Function f);
is it ok to change the object in Function f, like this :
struct AddOne
{
void operator()(int & x){x = x + 1;}
};
std::vector<int> vec(10);
std::for_ea...
Duplicate: http://stackoverflow.com/questions/717509/is-it-ok-to-mutate-objects-with-stdforeach
This is based on an earlier question which I can't seem to find anymore on stack overflow. Basically, is something like this legal?
struct doSomething
{
void operator()(int& i) {++i;}
};
int main()
{
std::vector<int> vec;
vec.pu...