这段代码有什么问题?
#include <map>
template<typename T>
struct TMap
{
typedef std::map<T, T> Type;
};
template<typename T>
T test(typename TMap <T>::Type &tmap_) { return 0.0; }
int _tmain(int argc, _TCHAR* argv[])
{
TMap<double>::Type tmap;
tmap[1.1] = 5.2;
double d = test(tmap); //Error: could not deduce template argument for T
return 0;
}
Run Code Online (Sandbox Code Playgroud) 如何使用C++使用三元运算符编写以下条件
int condition1, condition2, condition3;
int / double result; //int or double
....
std::cout << ( condition1: result1 : "Error" )
<< ( condition2: result2 : "Error" )
<< ( condition3: result3 : "Error")...;
Run Code Online (Sandbox Code Playgroud) 是否可以比较两个迭代器?使用std :: min的比较
void change ( typename TList <Item *>::Type ::iterator it_begin, typename TList <Item*>::Type ::iterator it_end )
{
....
this->items.resize ( index );
std::sort ( it_begin, std::min (it_end, it_begin += index - 1); //Compare two iterators, exception
....
}
Run Code Online (Sandbox Code Playgroud)
抛出以下异常:
Assertion failed: Vector iterators incompatible...
Run Code Online (Sandbox Code Playgroud)
比较还有其他方法吗?
我正在使用抽象类std :: ostream.有以下参考:
std::ostream &o = std::cout;
Run Code Online (Sandbox Code Playgroud)
如果满足任何条件,我需要初始化o,以便输出重定向到std :: cout.如果没有,输出将被重定向到文件
if (!condition)
o = file; //Not possible
Run Code Online (Sandbox Code Playgroud)
如何正确编写代码?
在这个例子中我对多态性的应用有些麻烦.这个问题类似于我的上一个问题
有3个抽象类:
class A
{
public:
virtual A * copy () const = 0;
virtual ~A() = 0;
};
A::~A(){}
class B
{
public:
virtual B * copy () const = 0;
virtual ~B() = 0;
};
B::~B(){}
class C: virtual public A , public B
{
public:
virtual C * copy () const = 0;
virtual ~C() = 0;
};
C::~C(){}
Run Code Online (Sandbox Code Playgroud)
和两个使用虚拟继承的继承类
class D: virtual public A
{
public:
virtual D * copy () const {return new D …Run Code Online (Sandbox Code Playgroud) c++ covariance virtual-inheritance visual-c++ visual-c++-2010
如何声明迭代器
std::map <T, Point <T> *> ,
Run Code Online (Sandbox Code Playgroud)
哪里:
template <typename T>
struct TList
{
typedef std::vector < std::map <T, Point <T> *> > Type;
};
Run Code Online (Sandbox Code Playgroud)
在以下代码中
int main ()
{
....
std::map <T, Point <T> *> ::iterator i_map; //Error
...
}
Run Code Online (Sandbox Code Playgroud)
g ++显示此错误:
error: dependent-name ` std::map<T,Point<T>*,std::less<_Key>,std::allocator<std::pair<const T, Point<T>*> > >::iterator' is parsed as a non-type, but instantiation yields a type
note: say `typename std::map<T,Point<T>*,std::less<_Key>,std::allocator<std::pair<const T, Point<T>*> > >::iterator' if a type is meant
Run Code Online (Sandbox Code Playgroud) 我正在使用模板和STL进行实验.这是我的测试代码......
template <typename Item>
struct TList
{
typedef std::vector <Item> Type;
};
template <typename Item>
class Cont
{
private:
typename TList <Item>::Type elements;
public:
void push_back ( const Item & p ) {elements.push_back ( p );}
typename TList <Item>::Type ::iterator
copy (typename TList <Item>::Type ::iterator first,
typename TList <Item>::Type ::iterator last,
typename TList <Item>::Type ::iterator result)
{
elements.resize(elements.size() + last - first); //Exception
return copy ( first, last, result );
}
typename TList <Item>::Type ::iterator begin() { return elements.begin(); } …Run Code Online (Sandbox Code Playgroud) 如何以最快的顺序将std :: multiset中的最后k个项目以相反的顺序复制到std :: vector?
c++ ×7
copy ×2
iterator ×2
templates ×2
vector ×2
compare ×1
conditional ×1
covariance ×1
multiset ×1
ostream ×1
parameters ×1
printing ×1
stdmap ×1
stl ×1
visual-c++ ×1