I have an std::set with the Compare class which requires additional parameter to compare keys. This variable parameter is determined in run-time and I pack it inside the set's keys just to make it accessible to Compare.
However, the parameter logically belongs to the set rather than the keys so this solution looks awkward and duplicates...
What would be the most elegant way too fix the following code:
#include <vector>
#include <map>
#include <set>
using namespace std;
typedef map< int, int > row_t;
typedef vector< row_t > board_t;
typedef row_t::iterator area_t;
bool operator< ( area_t const& a, area_t const& b ) {
return( a->first < b->first );
};
int main( int a...
I wish to insert into a c++ vector at a known position. I know the c++ library has an insert() function that takes a position and the object to insert but the position type is an iterator. I wish to insert into the vector like I would insert into an array, using a specific index.
...
I have a sorted collection of
class Thing
{
public:
item a;
item b;
other data;
};
vector<Thing> Things;
using
class MultiValuedComparator
{
public:
item a;
item b;
MultiValuedComparator(item c, item d)
{
a = c;
b = d;
}
};
Since I have duplicates of item a, and item b (but not other data), I...
Basically I'm doing the following:
std::set<int> indices;
// ..fill indices
if (flag)
{
// we need to process in ascending order
BOOST_FOREACH (int i, indices)
{
process(i);
}
}
else
{
// we need to process in descending order
BOOST_REVERSE_FOREACH (int i, indices)
{
process(i);
}
}
I wonder if ...
I came across a subtle bug a couple of days ago where the code looked something like this:
ostringstream ss;
int anInt( 7 );
ss << anInt << "HABITS";
ss << ends;
string theWholeLot = ss.str();
The problem was that the ends was sticking a '\0' into the ostringstream so theWholeLot actually looked like "7HABITS\0" (i.e. a null at the e...
I have data in the following format:
4:How do you do?
10:Happy birthday
1:Purple monkey dishwasher
200:The Ancestral Territorial Imperatives of the Trumpeter Swan
The number can be anywhere from 1 to 999, and the string is at most 255 characters long. I'm new to C++ and it seems a few sources recommend extracting formatted data with ...
Is there a way to posix_memalign a std::vector without creating a local instance of the vector first?
The problem I'm encountering is that I need to tell posix_memalign how much space to allocate and I don't know how to say
sizeof(std::vector<type>(n))
without actually creating a new vector.
Thanks
...
Is there an alternative version of std::find_if that returns an iterator over all found elements, instead of just the first one?
Example:
bool IsOdd (int i) {
return ((i % 2) == 1);
}
std::vector<int> v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
std::vector<int>::iterator it = find_if(v.begin(), v.end(), IsOd...
I have something that runs like this:
T baseline;
list<T>::const_iterator it = mylist.begin();
while (it != mylist.end()) {
if (it == baseline) /* <----- This is what I want to make happen */
// do stuff
}
My problem is that I have no idea how to extract the data from the iterator. I feel like this is a stupid thing to be con...
I am initializing a map map<string,int> ex;
in C++. I could not find contains_key or similar function in stl,
hence I am just using ex[inputString]++;
The debugger shows the int to be initialized to zero correctly, is it a good assumption?
...
#include <list>
#include <string>
using std::string;
using std::list;
int main()
{
list <string> list_;
list_.push_back("C");
list_.push_back("a");
list_.push_back("b");
list_.sort();
}
does sort() function sort the elements according to their character codes? I want the result here to be a b C after the sorting i...
Below is the class used as the value in a map:
class Book
{
int m_nId;
public:
// Book() { }
Inside main():
map
for( int i = 0; i ++i )
{
Book b( i );
mapBooks[ i ] = b;
}
The statement causing the error is:
mapBooks[ i ] = b;
If I add a default constructor, the error does not appear...
I'm working with a listview control which saves the data using AES encryption to a file. I need to keep the data of every item in listview in std::list class of std::string. should I just keep the data encrypted in std::list and decrypt to a local variable when its needed? or is it enough to keep it encrypted in file only?
...
I have some C code, where in there are two linked lists(say A and B) and A is inserted at a particular position into B and A still has elements.
How do I simulate the same behavior effectively using the C++ STL? If I try splice, it makes the second one empty.
Thanks,
Gokul.
...
This seems to me a simple question but I can't find the answer.
Currently I use an iterator to search through a vector and test its elements.
I access the elements using
std::vector<int>::iterator it;
if(*it==0);
Can I use the same pointer arithmetic style logic to also test the next element (without altering my iterator)?
I first n...
I am trying to model a network using C++. I have a struct called NetworkConnection:
struct NetworkConnection {
int i, j, weight;
}
and I have a class called Network
class Network {
public:
std::vector<NetworkConnection> connections_for(int i) {
return connections[i];
}
void connect(int i, int j, int weight) {
...
I have a simple requirement, i need a map of type . however i need fastest theoretically possible retrieval time.
i used both map and the new proposed unordered_map from tr1
i found that at least while parsing a file and creating the map, by inserting an element at at time.
map took only 2 minutes while unordered_map took 5 mins.
As...
I see function objects used often together with STL algorithms. Did function objects came about because of these algorithms? When do you use a function object in C++? What is its benefits?
...
This question is derived of the topic:
http://stackoverflow.com/questions/2280655/vector-reserve-c
I am using a datastructur of the type vector<vector<vector<double> > >. It is not possible to know the size of each of these vector (except the outer one) before items (doubles) are added. I can get an approximate size (upper bound) on th...