struct Node
{
int a;
int b;
};
Node node;
node.a = 2;
node.b = 3;
map<int, int> aa;
aa[1]=1; //O.K.
map<Node, int> bb;
bb[node]=1; //Compile Error
When I tried to map a struct into int, it gave me a compile error.
Why?
Thank you!
...
Here is the code:
#include <vector>
#include <iostream>
class A
{
public:
A() { std::cout << __FUNCTION__ << "\n"; }
~A() { std::cout << __FUNCTION__ << "\n"; }
A& operator=(const A&) { std::cout << __FUNCTION__ << "\n"; return *this;}
};
int main(int argc, char* argv[])
{
std::vector<A> as;
A a;
as.push_back(...
Apparently there is a “malloc_allocator” provided with gcc for use with STL. It simply wraps malloc and free. There is also a hook for an out-of-memory handler. Where can I find more about it? Where can I find its header file? I’m using gcc 4.x.
...
Let's say I have an array like this:
string x[2][55];
If I want to fill it with "-1", is this the correct way:
fill(&x[0][0],&x[2][55],"-1");
That crashed when I tried to run it. If I change x[2][55] to x[1][54] it works but it doesn't init the last element of the array.
Here's an example to prove my point:
string x[2][55];
...
I find the update operation on set tedious since there's no such an API on cplusplus. So what I currently do is sth like this:
//find element in set by iterator
Element copy = *iterator;
... // update member value on copy, varies
Set.erase(iterator);
Set.insert(copy);
Basically the iterator return by Set is a const_iterator and you ca...
I wrote a class that wraps an iterator and returns transformed values on demand:
// iterator-wrapper.h
template<class Iter, class Val, class Fct>
class IteratorWrapper {
Iter cur_;
const Iter last_;
const Fct fct_;
public:
IteratorWrapper(Iter first, Iter last, const Fct fct)
: cur_(first), last_(last), fct_(fct)
{}
con...
Given a functor appropriate for use with std::for_each and friends:
template <typename T> struct Foo {
void operator()(T const& t) { ... }
};
std::for_each(v.begin(), v.end(), Foo<Bar>());
Is there some standard way to convert this into an output iterator appropriate for use with std::copy and friends? (or the opposite adaptatio...
While working on some graphics code a while back, I wrote Rect and Region classes using ints as the underlying coordinate holder, and that worked fine. The Region was implemented as a simple class extension to an STL list, and just contains a list of Rects.
Now I also need the same kinds of classes using doubles as the underlying coordi...
The resize() function makes vector contain the required number of elements. If we require less elements than vector already contain, the last ones will be deleted. If we ask vector to grow, it will enlarge its size and fill the newly created elements with zeroes.
vector<int> v(20);
for(int i = 0; i < 20; i++) {
v[i] = i+1;
...
HI,
I started learning C++ STL's
i am just trying some small programs.one of them is below:
inline int const& max (int const& a, int const& b)
{
return a < b ? b : a;
}
template <typename T>
inline T const& max (T const& a, T const& b)
{
return a < b ? b : a;
}
int main()
{
::max(7, 42); // calls the nontemplate...
I'm currently stuck finding the correct syntax for trimming each string in a std::vector.
I tried
std::vector<std::string> v;
std::for_each(v.begin(), v.end(), &boost::trim);
which gave me the following error messages in MSVC7.1.
error C2784: '_Fn1 std::for_each(_InIt,_InIt,_Fn1)' : could not deduce template argument for 'T1' from ...
I have a class with a private data member of type vector< A*>.
The class has two public methods that actually use vector::size_type:
Method returning number of elements in the vector
Method returning element in the vector by index
I can add to public section of the class the following typedef:
typedef vector::size_type SIZE_t;
...
#include <iostream>
#include <cstring>
#include <string>
template <typename T>
inline T const& max (T const& a, T const& b)
{
return a < b ? b : a;
}
inline char const* max (char const* a, char const* b)
{
return std::strcmp(a,b) < 0 ? b : a;
}
template <typename T>
inline T const& max (T const& a, T const& b, T const& ...
The C++ standard prohibits declaring types or defining anything in namespace std, but it does allow you to specialize standard STL templates for user-defined types.
Usually, when I want to specialize std::swap for my own custom templated type, I just do:
namespace std
{
template <class T>
void swap(MyType<T>& t1, MyType<T>& t2)
{...
Maybe I am missing something completely obvious, but I can't figure out why one would use back_inserter/front_inserter/inserter,
instead of just providing the appropriate iterator from the container interface.
And thats my question.
...
I have always been an embedded software engineer, but usually at Layer 3 or 2 of the OSI stack. I am not really a hardware guy. I have generally always done telecoms products, usually hand/cell-phones, which generally means something like an ARM 7 processor.
Now I find myself in a more generic embedded world, in a small start-up, where ...
Encountered this problem before but forgot how I solved it.
I want to use the STL string class but the complier is complaining about not finding it.
Here is the complete .h file.
#ifndef MODEL_H
#define MODEL_H
#include "../shared/gltools.h" // OpenGL toolkit
#include <math.h>
#include <stdio.h>
#include <string>
#include <iostream>
...
I'm attempting to create C#-like multicast delegates and events using features from TR1. Or Boost, since boost::function is (mostly) the same as std::tr1::function. As a proof of concept I tried this:
template<typename T1>
class Event
{
private:
typedef std::tr1::function<void (T1)> action;
std::list<action> callbacks;
public:
inli...
I am new to templates in c++.
i was trying some small programs.
CPP [80]> cat 000001.cpp 000001.hpp
#include <iostream>
#include <string>
#include "000001.hpp"
int main()
{
int i = 42;
std::cout << "max(7,i): " << ::max(7,i) << std::endl;
double f1 = 3.4;
double f2 = -6.7;
std::cout << "max(f1,f2): " << ::max...
look at the following simple code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string s("1234567890");
string::iterator i1 = s.begin();
string::iterator i2 = s.begin();
string s1, s2;
s1.append(i1, ++i1);
s2.append(++i2, s.end());
cout << s1 << endl;
cout << s2 << endl;
}
w...