Attention please:
I already implemented this stuff, just not in any way generic or elegant. This question is motivated by my wanting to learn more tricks with the stl, not the problem itself.
This I think is clear in the way I stated that I already solved the problem, but many people have answered in their best intentions with solution...
I am writing (as a self-teaching exercise) a simple STL-Like range. It is an Immutable-Random-Access "container". My range, keeps only the its start element, the the number of elements and the step size(the difference between two consecutive elements):
struct range
{
...
private:
value_type m_first_element, m_element_count, m_step;
};...
If I code this
std::map<int, char> example = { (1,'a'),
(2, 'b'),
(3, 'c') };
then g++ says to me
deducing from brace-enclosed initializer list requires #include <initializer_list>
in C++98 ‘example’ must be initialized by constructor, not by ‘{...}’
and that annoys me ...
I have written those few line:
#include <vector>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
using namespace std;
template <class T> struct First
{
T num;
First() {}
First(const T &a) : num(a) {}
};
template <typename var> bool criterio(First<var> &primo, First<var> &secondo)
{
return (primo.num < se...
Greetings code-gurus!
I am writing an algorithm that connects, for instance node_A of Region_A with node_D of Region_D. (node_A and node_D are just integers). There could be 100k+ such nodes.
Assume that the line segment between A and D passes through a number of other regions, B, C, Z . There will be a maximum of 20 regions in betw...
I am trying to write a simple STL iterator for CArray MFC class using boost iterator adaptor. This is my code:
#include <boost/iterator/iterator_adaptor.hpp>
#include <afxtempl.h>
class CArrIter : public boost::iterator_adaptor< CArrIter ,
int,
int,
boost::random_access_traversal_tag >
{
public:
CArrIter(CArray<int,in...
I have to correct some C++/STL code. Unfortunately I have very little C++ experience and know nothing about STL. Nevertheless I finished most of it, but the function below is still giving me problems:
C++ source:
double MyClass::CalculateAvg(const std::list<double> &list)
{
double avg = 0;
std::list<int>::iterator it;
for(i...
I know several (all?) STL implementations implement a "small string" optimization where instead of storing the usual 3 pointers for begin, end and capacity a string will store the actual character data in the memory used for the pointers if sizeof(characters) <= sizeof(pointers). I am in a situation where I have lots of small vectors wit...
I'm having an issue where using vector.push_back(value) is overwriting the final value, rather than appending to the end. Why might this happen? I have a sample item in the vector, so it's size never hits zero. Below is the code..
void UpdateTable(vector<MyStruct> *Individuals, MyStruct entry)
{
MyStruct someEntry;
bool isNew...
This code suffers from overflow because the type of intermediate results does not depend on the destination type:
vector< uint8_t > increments;
…
vector< uint32_t > increasing( increments.size() );
partial_sum( increments.begin(), increments.end(), increasing.begin() );
However, so does this (GCC 4.2):
partial_sum( increments.begin()...
I'm writing a C++ custom allocator for use with STL. When I put the following code in the class definition, it compiles:
#include "MyAlloc.hpp"
#if 1
template <typename T>
typename MyAlloc<T>::pointer
MyAlloc<T>::allocate(size_type n, MyAlloc<void>::const_pointer p) {
void *ptr = getMemory(n*sizeof(T));
typename MyAlloc<T>::pointe...
For those who are following the saga, I am still trying to define Finite State Machine, states & events in the "proper" C++ way, with templates.
What's wrong with this code?
template <typename StateTypeEnum, typename EventTypeEnum>
class Fsm
{
public:
Fsm(E_subSystems subSystem,
uint8_t instance,
const char * cons...
Possible Duplicates:
Hashtable in C++?
can anybody offer a simple hash_map example in C++?
Does the STL contain an implementation of a hashtable?
If so, can you provide a brief example of how to use it?
...
There's simple example:
#include <vector>
int main() {
vector<int> veci;
vector<double> vecd;
for(int i = 0;i<10;++i){
veci.push_back(i);
vecd.push_back(i);
}
vecd = veci; // <- THE PROBLEM
}
The thing I need to know is how to overload operator = so that I could make assignment like this:
vector<double> = vector<int>;
I'...
I've doing some performance analysis on the software I develop, and I've found that lookups on a global dictionary of URL's takes about 10% of the application's "load" phase time. The dictionary is implemented as a C++ STL std::map, which has O(lg n) lookups. I'm going to move it to a hash_map, which has roughly fixed time lookups. Th...
how would you check if the iterator that was returned by the function points to something in container class?
...
I've read here and other places that when iterating a std::vector using indexes you should:
std::vector <int> x(20,1);
for (std::vector<int>::size_type i = 0; i < x.size(); i++){
x[i]+=3;
}
But what if you are iterating two vectors of different types:
std::vector <int> x(20,1);
std::vector <double> y(20,1.0);
for (std::vector<int>:...
I wonder if there is support in STL for this:
Say I have an class like this :
class Person
{
public:
int getAge() const;
double getIncome() const;
..
..
};
and a vector:
vector<Person*> people;
I would like to sort the vector of people by their age:
I know I can do it the following way:
class AgeCmp
{
public:
bool oper...
Let v1 be the target vector, v2 needs to be appended to the back of it.
I'm now doing:
v1.reserve(v1.size() + v2.size());
copy(v2.begin(), v2.end(), back_inserter(v1));
Is this the most efficient way? Or can it maybe be done just via copying a chunk of memory?
Thanks!
...
Hi,
I can sort a int* array using stl,
plain and simple like
std::sort(myarray, myarray + size);
Is there any equal simple way to randomize it?
thanks
...