Which of these 2 methods is better and why?
Method 1:
void fun(int i) {
//do stuff
}
...
for_each(a.begin(), a.end(), fun);
Method 2:
class functor {
public:
void operator()(int i);
};
...
for_each(a.begin(), a.end(), functor());
Edit: Should have formulated it this way, in what situation is one of the above method preferabl...
Hello :)
I'm trying to write my first iterator class / container type. Basically, I want to be able to iterate though a file, have the file converted to HEX on the fly, and pass the result into the boost::xpressive library. I don't want to do a one shot conversion to a string in RAM, because some of the files I need to be able to proces...
Consider the following:
typedef struct {
int a;
int b;
int c;
int d;
} ABCD;
typedef std::vector<ABCD> VecABCD;
Say I wanted to add up every 'a' member in a vector of type VecABCD. Easy! I just loop through the vector, and sum as I go.
int CalcSumOfA(const VecABCD &vec)
{
int sumOfA = 0;
VecABCD::const_iter...
No boost, just plain STL please.
I have a class Foo* mapping to a set of pointers of class ID.
and I need to map a pointer to an instance of ID to a FOO class.
say I have this function:
void FindXXX(const ID* pID)
{
/*find the containing FOO class quickly, but not at expense of an ugly code*/
}
right now I map each ID* to FOO...
Allegedly you cannot just erase/remove an element in a container while iterating as iterator becomes invalid. What are the (safe) ways to remove the elements that meet a certain condition? please only stl, no boost or tr1.
Thanks
EDIT
Is there a more elegant way if I want to erase a number of elements that meet a certain criteria, perh...
I mean, aside from its obligating name (the Standard Template Library)...
C++ initially intended to present OOP concepts into C. That is: you could tell what a specific entity could and couldn't do (regardless of how it does it) based on its class and class hierarchy. Some compositions of abilities are more difficult to describe in this...
I need to take a C++ vector with potentially a lot of elements, erase duplicates, and sort it. Looks like this code will do the trick: (Correction--it won't; next time I'll test before posting. Thanks for the feedback.)
vec.erase(
std::unique(vec.begin(), vec.end()),
vec.end());
std::sort(vec.begin(), vec.end());
Is it f...
Hi All,
In a recent interview, I was asked to implement a thread safe generic (i.e.template based) stack in C++, on linux machine.
I quickly came up with the following (It may have compilation errors).
I got through. The interviewer probably liked something in this implementation. Maybe the design part :)
Here are a few problems that th...
I googled quite a while in order to find out a comparison that shows the differences in complexity for all STL-Containers on insert/push erase/pop etc. I did not find any. Also not in all of my STL Books. Any hint?
I know some rules of thumb of course. But where is a definition?
...
I would like to know whether WinInet or WinHttp will get webpage contents quickly or is there any other quicker method for getting webpage content (less than 1 sec).
The programming environment which I am using is VC++
...
I'm intersecting some sets of numbers, and doing this by storing a count of each time I see a number in a map.
I'm finding the performance be very slow.
Details:
- One of the sets has 150,000 numbers in it
- The intersection of that set and another set takes about 300ms the first time, and about 5000ms the second time
- I haven't don...
Hi
Can anybody tell me how to increment the iterator by 2 times.
iter++ is available - I have to do iter+2 how can I achieve this.
...
In this question asking how to adjust the iterator to an STL container by 2 elements two different approaches are offered:
either use a form of arithmetic operator - +=2 or ++ twice
or use std::advance()
I've tested both of them with VC++ 7 for the edge case when the iterator points onto the last element of the STL container or beyon...
I'm intersecting a set of 100,000 numbers and a set of 1,000 numbers using set_intersection in STL and its taking 21s, where it takes 11ms in C#.
C++ Code:
int runIntersectionTestAlgo()
{
set<int> set1;
set<int> set2;
set<int> intersection;
// Create 100,000 values for set1
for ( int i = 0; i < 100000; i++ )
...
Using the STL C++ hash_map...
class MyKeyObject
{
std::string str1;
std::string str2;
bool operator==(...) { this.str1 == that.str1 ... }
};
class MyData
{
std::string data1;
int data2;
std::string etcetc;
};
like this...
MyKeyObject a = MyKeyObject(...);
MyData b = MyData(...);
stdext::hash_map <MyKeyObjec...
I'm running the following code, using Visual Studio 2008 SP1, on Windows Vista Business x64, quad core machine, 8gb ram.
If I build a release build, and run it from the command line, it reports 31ms. If I then start it from the IDE, using F5, it reports 23353ms.
Here are the times: (all Win32 builds)
DEBUG, command line: 421ms
DEBUG...
On my machine (Quad core, 8gb ram), running Vista x64 Business, with Visual Studio 2008 SP1, I am trying to intersect two sets of numbers very quickly.
I've implemented two approaches in C++, and one in C#. The C# approach is faster so far, I'd like to improve the C++ approach so its faster than C#, which I expect C++ can do.
Here is t...
I'm just experimenting with boost::pool to see if its a faster allocator for stuff I am working with, but I can't figure out how to use it with boost::unordered_map:
Here is a code snippet:
unordered_map<int,int,boost::hash<int>, fast_pool_allocator<int>> theMap;
theMap[1] = 2;
Here is the compile error I get:
Error 3 error C2064...
I am extending the Visual Studio 2003 debugger using autoexp.dat and a DLL to improve the way it displays data in the watch window. The main reason I am using a DLL rather than just the basic autoexp.dat functionality is that I want to be able to display things conditionally. e.g. I want to be able to say "If the name member is not an em...
Hi
I have a map
map < string , list < string > > mapex ; list< string > li;
how to display the above map items on the console.
...