How can i loop thru a stl::List and store the value of one of the objects for use later in the function?
Particle *closestParticle;
for(list<Particle>::iterator p1 = mParticles.begin(); p1 != mParticles.end(); ++p1 )
{
// Extra stuff removed
closestParticle = p1; // fails to compile (edit from comments)
}
...
i want to sort a vector
vector<myclass> object;
where as myclass contains many int data variables, how can i sort my vector on any specific data variable of myclass.
...
My comments on this answer got me thinking about the issues of constness and sorting. I played around a bit and reduced my issues to the fact that this code:
#include <vector>
int main() {
std::vector <const int> v;
}
will not compile - you can't create a vector of const ints. Obviously, I should have known this (and intellect...
From a previous question about vector capacity, Mr. Bailey said:
In current C++ you are guaranteed that no reallocation occurs after a call to reserve until an insertion would take the size beyond the value of the previous call to reserve. Before a call to reserve, or after a call to reserve when the size is between the value of the ...
Profiling my cpu-bound code has suggested I that spend a long time checking to see if a container contains completely unique elements. Assuming that I have some large container of unsorted elements (with < and = defined), I have two ideas on how this might be done:
The first using a set:
template <class T>
bool is_unique(vector<T> X) {...
In a plugin-based C++ project, I have a TmpClass that is used to exchange data between the main application and the plugins. Therefore the respective TmpClass.h is included in the abstract plugin interface class that is included by the main application project, and implemented by each plugin.
As the plugins work on STL vectors of TmpCla...
I attempted to do something like this but it does not compile:
class point
{
public:
int x;
int y;
};
int main()
{
vector<point> vp1;
vector<point> vp2;
vector<point> vp3;
map < vector<point>, int > m;
m[vp1] = 1;
m[vp2] = 2;
m[vp3] = 3;
map < vector<point>, int >::iterator it;
...
I have a map of objects and I want to update the object mapped to a key, or create a new object and insert into the map. The update is done by a different function that takes a pointer to the object (void update(MyClass *obj))
What is the best way to "insert or update" an element in a map?
...
I'm a little bit scared about something like this:
std::map<DWORD, DWORD> tmap;
tmap[0]+=1;
tmap[0]+=1;
tmap[0]+=1;
Since DWORD's are not automatically initialized, I'm always afraid of tmap[0] being a random number that is incremented. How does the map know hot to initialize a DWORD if the runtime does not know how to do it?
I...
Hi,
I have this destructor that create error at runtime "vector iterator not dereferencable".
The gridMatrix is a std::vector<std::vector<std::vector<AtomsCell< Atom<T> * > * > * > * >
I added the typename and also the typedef but I still have the error.
I will move for this idea of vect of vect* of vect* to use boost::multi_array I ...
I am currently building my own toy vector for fun, and I was wondering if there is something like the following in the current or next standard or in Boost?
template<class T>
void destruct(T* begin, T* end)
{
while (begin != end)
{
begin -> ~T();
++begin;
}
}
template<class T>
T* copy_construct(T* begin, T* ...
set<int> s;
s.insert(1);
s.insert(2);
...
s.insert(n);
I wonder how much time it takes for s.find(k) where k is a number from 1..n?
I assume it is log(n). Is it correct?
...
As I know that accessing an element in vector takes constant time while in map takes logarithmic time. However, storing a map takes less memory than storing a vector.
Therefore, I want to ask which one is better in general? I'm considering using one of those two in my program, which has about 1000 elements. I plan to use 3 dimensional v...
Hi,
I am attempting to compile QtScriptGenerator (gitorious) with Visual Studio 2010 (C++) and have run into a compile error. In searching for a solution, I have seen occasional references to compile breakages introduced since VS2008 due to changes in VS2010's implementation of STL and/or c++0x conformance changes.
Any ideas what is h...
My example is as below. I found out the problem is with "const" in function void test's parameter. I don't know why the compiler does not allow. Could anybody tell me? Thanks.
vector<int> p;
void test(const vector<int> &blah)
{
vector<int>::iterator it;
for (it=blah.begin(); it!=blah.end(); it++)
{
cout<<*it<<" ";
}
}...
Let's say I have:
class myClass
std::list<myClass> myList
where myClass does not define the == operator and only consists of public fields.
In both VS2010 and VS2005 the following does not compile:
myClass myClassVal = myList.front();
std::find( myList.begin(), myList.end(), myClassVal )
complaining about lack of == operator.
I n...
So, I was writing a C++ program which would allow me to take control of the entire world. I was all done writing the final translation unit, but I got an error:
error C3848: expression having type 'const `anonymous-namespace'::ElementAccumulator<T,BinaryFunction>' would lose some const-volatile qualifiers in order to call 'void `anonymo...
Hi all.
I wanted to write a higher order function filter with C++. The code I have come up with so far is as follows:
#include <iostream>
#include <string>
#include <functional>
#include <algorithm>
#include <vector>
#include <list>
#include <iterator>
using namespace std;
bool isOdd(int const i) {
return i % 2 != 0;
}
template <
...
Hi,
How to implement the ls "filename_[0-5][3-4]?" like class? The result I would like to store in the vector.
Currently I am using system() which is calling ls, but this is not portable under MS.
thanks,
Arman.
...
Hello,
I've been using std::string's == operator for years on windows and linux. Now I am compiling one of my libraries on linux, it uses == heavily. On linux the following function fails, because the == returns false even when the strings are equal (case sensitive wise equal)
const Data* DataBase::getDataByName( const std::string& na...