Good Day,
Assume that I am writing a Python-like range in C++. It provides all the characteristics of Random Access containers(Immutable of course). A question is raised in my mind about the following situation:
I have two different iterators, that point to different instances of the range container. The thing is that these two ranges ...
I can create an array initialized with elements like this:
int a[] = {10, 20, 30};
How do I create an STL vector and initialize it like the above? What is the best way to do so with the minimum typing effort?
The best I can do is:
std::vector<int> ints;
ints.push_back(10);
ints.push_back(20);
ints.push_back(30);
Though I could sh...
I'm looking for a C++ data structure that will let me associate objects with a unique numeric value (a key), and that will re-use these keys after the corresponding object have been removed from the container. So it's basically somewhat of a hybrid map/queue structure. My current implementation is a
std::map<size_t, TObject>
and I ins...
Hi,
I have a class looking like this:
#include <vector>
#include "record.h"
#include "sortcalls.h"
template<
typename T,
template<typename , typename Allocator = std::allocator<T> > class Cont = std::vector>
class Sort: public SortCall {
This code is working and I'm calling it like this from other classes:
Comparator c; // c...
The code below gives the error:
error: type ‘std::list<T,std::allocator<_Tp1> >’ is not derived from type ‘Foo<T>’
error: expected ‘;’ before ‘iter’
#include <list>
template <class T> class Foo
{
public:
std::list<T>::iterator iter;
private:
std::list<T> elements;
};
Why and what should this be to be correct?...
I'm defining a vector as:
vector< int, MyAlloc< int> > *v = new vector< int, MyAllooc< int> > (4);
MyAlloc is allocating space for only 4 ints. Memory for _M_start, _M_finish, and _M_end_of_storage is being allocated on the heap before the memory for the 4 ints. But who is allocating this memory for _M_start, _M_finish, and _M_end_of_...
I am working with a memory manager that, on occasion, wants to defragment memory. Basically, I will go through a list of objects allocated by the memory manager and relocate them:
class A {
SomeClass* data; // This member is allocated by the special manager
};
for(... each instance of A ...)
a.data = memory_manager.relocate(a.da...
If one implements an array class the way it is commonly implemented, its performance is slower compared to its STL equivalent like a vector. So what makes STL containers/algorithms fast?
...
I know technically the std::basic_string template is not required to have contiguous memory. However, I'm curious how many implementations exist for modern compilers that actually take advantage of this freedom. For example, if one wants code like the following it seems silly to allocate a vector just to turn around instantly and return ...
I would like to write something like this, which cannot be compiled:
std::vector<A> as;
std::vector<B> bs( as.size() );
std::transform( as.beginn(), as.end(), bs.begin(), boost::lexical_cast<B> );
But this is not working, so I created a functor which is doing this for me:
template<typename Dest>
struct lexical_transform
{
templat...
Hi,
Some times we have to put different objects in the same hierarchy in one container. I read some article saying there are some tricks and traps. However, I have no big picture about this question. Actually, this happens a lot in the real word.
For example, a parking lot has to contain different types of cars; a zoo has to contain d...
I'm trying to use a std::list in my objective-c program and having run-time issues.
I have (edit for brevity)
#import <list>
...
@interface Foo {
std::list<Bar *> *myList;
}
...
@implementation Foo
-(void)loopList {
myList = new std::list<Bar *>;
for (std::list<Bar *>::iterator ii; ii != myList->end(); ++ii) ...
Hi,
I'm working on a module which uses a shared library, which in turn has a static library linked to it. The shared library build works fine and generates a .so. When I try to use it in the module, I get a variety of errors, most of which are based on stl (stl collections to be specific), at the compilation stage. The errors look like:
...
This is my code
map<string,int> persons;
persons["B"] = 123;
persons["A"] = 321;
for(map<string,int>::iterator i = persons.begin();
i!=persons.end();
++i)
{
cout<< (*i).first << ":"<<(*i).second<<endl;
}
Expected output:
B:123
A:321
But output it gives is:
A:321
B:123
I want it to maintain the order in whi...
Consider the following snippet:
class Foo {
public:
Foo( int Value );
// other stuff
};
std::list< boost::shared_ptr< Foo > > ListOfFoo = list_of( 1 )( 2 )( 3 )( 4 )( 5 );
This does not work out of the box. What is the simplest way to make this work, or is there any method to assign values to ListOfFoo as simple as that?
...
I'm trying to do a double-loop over a std::list to operate on each pair of elements. However, I'm having some trouble initialising the second iterator. The code I'd like to write is:
for(std::list<int>::iterator i = l.begin(); i != l.end(); ++i) {
for(std::list<int>::iterator j = i+1; j != l.end(); ++j) {
...
}
}
That ...
Consider the following:
std::vector<int> vec(1); // vector has one element
std::fill(vec.begin(), vec.begin(), 42);
std::fill(vec.begin()+1, vec.end(), 43);
std::fill(vec.end(), vec.end(), 44);
Will all of the std::fill usages above result in defined behavior? Am I guaranteed that vec will remain unmodified? I'm inclined to think "yes...
After a lot of investigations with valgrind, I've made the conclusion that std::vector makes a copy of an object you want to push_back.
Is that really true ? A vector cannot keep a reference or a pointer of an object without a copy ?!
Thanks
...
Given the initialized variables unsigned a, unsigned b with b > a and std::vector<std::string> strings of size b-a. How can I fill strings with the elements e.g. "x3" "x4" "x5" "x6" (in case a=3 and b=7) for arbitrary a and b with one C++ command (meaning one semicolon at all :))?
...
I've declared a template class MyContainer as bellow, then created an instance of it of type DataType1. The DataType1 class provides a friend function "DataSpecificComparison" which is used by std::sort to compare DataType1 objects. The program compiled and sorted correctly.
I then defined a class called DataType2, gave it a friend imp...