I have a class (foo) that contains a vector.
If i try iterating over the elements in the vector like so:
for(vector<random>::iterator it = foo.getVector().begin();
it != foo.getVector().end(); ++it) {
cout << (*it) << endl;
}
The first element is always corrupted and returns garbage data.
However, if do something like:
...
Is there a C/C++/STL/Boost clean method to convert a date time string to epoch time (in seconds)?
yyyy:mm:dd hh:mm:ss
...
Let's say I have a sequential container, and a range (pair of iterators) within that container of elements that are currently 'active'. At some point, I calculate a new range of elements that should be active, which may overlap the previous range. I want to then iterate over the elements that were in the old active range but that are not...
Confusing title, hopefully some code will clarify:
struct MyNestedType {
void func();
};
struct MyType {
MyNestedType* nested;
}
std::vector<MyType> vec;
// ... populate vec
// I want something approximating this line, but that doesn't use made-up C++!
std::for_each(vec.begin(), vec.end(), std::mem_fun_ref(&MyType::nested->f...
I have just found out that when I search a map like :
std::map<std::string, int> aMap;
the keys I search start to be part of the map. In the case above the values are stored as zeros. In case of pointers it stores the values as 0 valued pointers
I am doing the search with the [] operator, like in :
int a = aMap["some key"];
Ca...
In this chunk of code I add a pair on a map and everything is fine but when I delete a pair that isn't the last one the map doesn't add any more pairs. What I'm Doing wrong??
SomeClass::add(Object object)
if (!object.empty())
{
ObjectList::iterator result = find(object.name());
if (result == ObjectList.end())
{
object.orde...
I want to find the first item in a sorted vector that has a field less than some value x.
I need to supply a compare function that compares 'x' with the internal value in MyClass but I can't work out the function declaration.
Can't I simply overload '<' but how do I do this when the args are '&MyClass' and 'float' ?
float x;
std::vect...
I have implemented this code, but fail at compilation (VC2008 Express Ed.) :
Now all code is here.
#include <stdio.h>
#include <vector>
#include <string>
#include <hash_map>
using namespace std;
using namespace stdext;
typedef vector type_key;
int main(int argc, char* argv[])
{
type_key key;
hash_map<type_key, string> map_s...
If I have a std::vector and I want to delete the x'th element how to do it?
std::vector<int> vec;
vec.push_back(6);
vec.push_back(-17);
vec.push_back(12);
vec.erase(???);
Please help!
...
While reading the answers to this question I got a doubt regarding the default construction of the objects in the vector. To test it I wrote the following test code:
struct Test
{
int m_n;
Test();
Test(const Test& t);
Test& operator=(const Test& t);
};
Test::Test() : m_n(0)
{
}
Test::Test(const Test& t)
{
m_n =...
Hey all,
ok, so I got a doubt, I want to know if this is possible:
I'm using a database, with generic data (strings, ints, bools, etc...). Whenever an object is constructed or a member of an object is modified, I have to query the database with the specific action (SELECT or UPDATE).
First of all, this isn't a DB related question, my r...
I am running a simulation with a lot if bunch of initial memory allocations per object. The simulation has to run as quickly as possible, but the speed of allocation is not important. I am not concerned with deallocation.
Ideally, the allocator will place everything in a contiguous block of memory. (I think this is sometimes called a...
I have 8 sorted lists that I need to merge into 1 sorted list. I don't know the best way to do this. I was thinking of the following:
void merge_lists_inplace(list<int>& l1, const list<int>& l2)
{
list<int>::iterator end_it = l1.end();
--end_it;
copy(l2.begin(), l2.end(), back_inserter(l1));
++end_it;
inplace_merge(l...
I can do:
map<char*, int> counter;
++counter["apple"];
But when I do:
--counter["apple"] // when counter["apple"] ==2;
I got debugger hung up in VS 2008.
Any hints?
...
Dears,
Is there a way finding largest container inside a container using STL? ATM, I have this
rather naïve way of doing it:
int main()
{
std::vector<std::vector<int> > v;
...
unsigned int h = 0;
for (std::vector<std::vector<int> >::iterator i = v.begin(); i != v.end(); ++i) {
if (*i...
Hi,
I'm using std::map to store a lot of elements (pairs of elements) and I have a "little" doubt, what is more efficient to iterate all elements over my std::map, iterator or reverse_iterator?
Thank's.
Salu2.
...
Hi,
I've got a very simple map :
std::map<int, double> distances;
distances[20.5] = 1;
distances[19] = 2;
distances[24] = 3;
How do i know if there isn't any returned value, when using a map::upper_bound() in this case for example:
std::map<int, double>::iterator iter = distances.upper_bound(24);
(24 is the max key so an unexpecte...
I have a C++ std::vector denoted as:
std::vector<GameObject*> vectorToSort;
Each object in vectorToSort contains a float parameter which is returned by calling "DistanceFromCamera()":
vectorToSort.at(position)->DistanceFromCamera();
I wish to sort the vector by this float parameter however std::sort does not appear to be able to do...
Something that takes 1 second to run on Linux takes 45 seconds to run on AIX. I haven't dug directly into that code but as a test grabbed a small application that does very little from another SO question:
int main ( int argc, char **argv)
{
int i = 0;
std::vector<int> vec;
vec.push_back(6);
vec.push_back(-17);
vec.push_back(12);
for ...
This is quite strange for me, but I'm getting an unexpected and random segmentation fault when I launch my program. Some times it works, some times it crashes.. The debugger of Dev-C++ points me to a line of the file : stl_construct.h
/**
* @if maint
* Constructs an object in existing memory by invoking an allocated
* object's ...