I have a simple struct
struct A {
int t;
int s;
};
Those were contained in std::list<A>. The problem is that I group A depending on s. I can avoid duplication of s if I'd wrap A in another struct and keep s in a separated container. This has a lot of benefits (e.g. I don't need to do stupid and potentially dangerous things like AC...
I have a std::map that I use to map values (field ID's) to a human readable string. This map is initialised once when my program starts before any other threads are started, and after that it is never modified again. Right now, I give every thread its own copy of this (rather large) map but this is obviously inefficient use of memory and...
Hi,
In my application I have to derive some classes from a base one, the problem is that I want to enforce the derived classed to have 3 particular constructor implementation. As c++ don't have virtual pure constructor, it seemed quite desperate (I had to check manually each class implementation to ensure that the particular ctors are i...
I recently learned about the right way to work with reverse iterators in C++ (specifically when you need to erase one). (See this question and this one.)
This is how you're supposed to do it:
typedef std::vector<int> IV;
for (IV::reverse_iterator rit = iv.rbegin(), rend = iv.rend();
rit != rend; ++rit)
{
// Use 'rit' if a rever...
First_Layer
I have a win32 dll written in VC++6 service pack 6. Let's call this dll as FirstLayer. I do not have access to FirstLayer's source code but I need to call it from managed code. The problem is that FirstLayer makes heavy use of std::vector and std::string as function arguments and there is no way of marshaling these types int...
is there any code to find the maximum value of integer (which is acccording to the compiler) in c/c++ like Integer.MaxValue function in java
...
I'm attempting a simple test of binary file I/O using the STL copy algorithm to copy data to/from containers and a binary file. See below:
1 #include <iostream>
2 #include <iterator>
3 #include <fstream>
4 #include <vector>
5 #include <algorithm>
6
7 using namespace std;
8
9 typedef std::ostream_iterator<double> oi_t;
10 typed...
I assumed that std::wstring and std::string both provide more or less the same interface.
So I tried to enable unicode capabilities for our application
# ifdef APP_USE_UNICODE
typedef std::wstring AppStringType;
# else
typedef std::string AppStringType;
# endif
However that gives me a lot of compile errors when -DAPP_USE_UNI...
I have the need to store a list/collection/array of dynamically created objects of a certain base type in C++ (and I'm new to C++). In C# I'd use a generic collection, what do I use in C++?
I know I can use an array:
SomeBase* _anArrayOfBase = new SomeBase[max];
But I don't get anything 'for free' with this - in other words, I can't...
Whenever someone starts using the STL and they have a vector, you usually see:
vector<int> vec ;
//... code ...
for( vector<int>::iterator iter = vec.begin() ;
iter != vec.end() ;
++iter )
{
// do stuff
}
I just find that whole vector<int>::iterator syntax sickitating. I know you can typedef vector<int>::iterator VecI...
Extends.
I have:
struct Coord
{
int row, col ;
bool operator<( const Coord other.row other.col ;
}
} ;
I'm trying to create a map<Coord, Node*>, where you can look up a Node* by Coord.
The problem is, it has bugs. Lookups into the map<Coord, Node*> by Coord are returning the wrong ones.
I'm having difficulty figuring out ...
The class std::wstring is missing some operations for "normal" c strings (and literals).
I would like to add these missing operations in my own custom class:
#include <string>
class CustomWString : public std::wstring {
public:
CustomWString(const char*);
const char* c_str(void);
};
The code above c...
I came across one requirement where the record is stored as
Name : Employee_Id : Address
where Name and Employee_Id are supposed to be keys that is, search function is to be provided on
both Name and Employee Id.
I can think of using a map to store this structure
std::map< std:pair<std::string,std::string> , std::string >
// ...
Hi,
Studying STL I have tried to negate a functor with not2 but encontered problems.
Here is the example:
#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;
struct mystruct : binary_function<int,int,bool> {
bool operator() (int i,int j) { return i<j; }
};
template <class T>
class gen...
I've made a method to scroll/wrap around a map of items, so that if the end is reached, the method returns the first item and vice-versa.
Is there more succinct way of doing this?
MyMap::const_iterator it = myMap.find(myKey);
if (it == myMap.end())
return 0;
if (forward) {
it++;
if (it == myMap.end()) {
it = myM...
I have been working with C++ for a few years now and have got good theoretical knowledge on the matter (I think).
However I've been missing involvement in good projects, sort of projects that really gets one going on the technologies.
So I intend to work on my own to get some good grip on C++ and related technologies.
'Have started with ...
Are there STL implementations that use operator new[] as an allocator? I was just wondering because on my compiler, making Foo::operator new[] private did not prevent me from creating a vector<Foo>... is that behavior guaranteed by anything?
...
Hi.
Studing STL I have written a a simple program to test functors and modifiers. My question is about the difference aon using CLASS or STRUCT to write a functor and try to operate on it with function adaptors.
As far as I understand in C++ the difference beetween CLASS and STRUCT is that in the last case the members are public by defau...
I have a vector like so:
vector<MyType*> _types;
And I want to iterate over the vector and call a function on each of MyTypes in the vector, but I'm getting invalid return errors from the compiler. It appears the pos iterator isn't a pointer to MyType, it's something else. What am I not understanding?
Edit: Some code..
for (pos =...
I'm checking the results from the static code analysis tool Klocwork.
It complains about the following code:
293 for( my_vector_typedef::iterator it( start_pos ); it != end_pos ; ++it ){
294 delete *it;
295 }
With the following message:
Object 'it._M_current' is used after it was freed. Object 'it._M_current' was used at line 293 ...