enum Reaction{single,chain};
class X
{
X* parent_;
X* left_;
X* right_;
Reaction* reaction_;//this pointer points from every obj to the same place, cannot be static
};
Run Code Online (Sandbox Code Playgroud)
问题是:如何设计析构函数以便只删除一次reaction_?
此代码(不能"正常"工作)但在VS 2010中编译但不会在GCC 4.5.1中编译
#include <iostream>
#include <vector>//not necessary second > should skip like brackets
using namespace std;
template<class ForwardIterator>
void iterator_swap(ForwardIterator& left,ForwardIterator& right)
{
typename ForwardIterator::value_type tmp = *left;
*left = *right;
*right = tmp;
}
template<class T>
std::ostream& operator<<(std::ostream& out, const std::vector<T>& obj)
{
typename std::vector<T>::const_iterator beg = obj.cbegin();
typename std::vector<T>::const_iterator end = obj.cend();
while (beg != end)
{
out << *beg << '\n';
++beg;
}
return out;
}
int main()
{
vector<unsigned> v_1;
for (vector<unsigned>::size_type i = 0; …Run Code Online (Sandbox Code Playgroud) 这个:
cout << std::is_const<const int*>::value;
Run Code Online (Sandbox Code Playgroud)
打印错误,我认为它应该打印真实.为什么打印错误?
为什么这段代码有两个不同的输出(GCC 4.5.1)(我评论过重要的一行):
int main()
{
bool a = 1;
bool b = 1;
bool c = 1;
bool a_or_b = (a || b);
bool not_a_or_b = !a_or_b;
bool not_a_or_b__c = not_a_or_b || c;
cout << "(a || b): " << (a || b) << '\n';
cout << "!(a || b): " << !(a || b) << '\n';
cout << "!(a || b) || c: " << (!(a || b)) || c << '\n';//HERE I'M GETTING 0 (incorrectly I would say) …Run Code Online (Sandbox Code Playgroud) 为什么这不起作用?
template <class T>
struct Low;
template <>
struct Low<int> {};//Here I'm trying to specialize for int
int main()
{
Low<1> a;
}
Run Code Online (Sandbox Code Playgroud) uint64_t source = numeric_limits<uint64_t>::max();
int64_t target = source;
BOOST_CHECK(source != target);//THIS SHOULD CHECK AS true - target != source
Run Code Online (Sandbox Code Playgroud)
此检查失败但它应该通过 - source与目标不同.
这是从codility中获取的任务,需要O(n)复杂性.虽然我已经解决了它给出正确结果的任务,但时间复杂度为O(n*n).在解释如何将复杂性降低到O(n)时,将不胜感激.
任务描述:
给出了由N个整数组成的非空零索引数组A. 数组A表示磁带上的数字.
任何整数P,使得0 <P <N,将该磁带分成两个非空部分:A [0],A [1],...,A [P-1]和A [P],A [ P + 1],...,A [N - 1].
两部分之间的差异是:|(A [0] + A [1] + ... + A [P - 1]) - (A [P] + A [P + 1] + .. .+ A [N - 1])|
换句话说,它是第一部分的总和与第二部分的总和之间的绝对差.
例如,考虑数组A,使得:
A [0] = 3 A [1] = 1 A [2] = 2 A [3] = 4 A [4] = 3
我们可以在四个地方拆分这个磁带:
Run Code Online (Sandbox Code Playgroud)P = 1, difference = |3 ? 10| = 7 P = …
我定义了两个类:
class Token_
{
public:
virtual char operator*()const = 0;//this fnc cannot run implicitly
protected:
Token_()
{ }
Token_(const Token_&);
Token_& operator=(const Token_&);
};
Run Code Online (Sandbox Code Playgroud)
第二个:
class Operator : public Token_
{
public:
Operator(const char ch):my_data_(token_cast<Operator_enm>(ch))
{ }
Operator_enm get()const
{
return my_data_;
}
Operator_enm set(const Operator_enm& value)
{
Operator_enm old_value = get();
my_data_ = value;
return old_value;
}
char operator*()const//this operator has to be invoke explicitly
{
return static_cast<char>(my_data_);
}
private:
Operator_enm my_data_;
};
Run Code Online (Sandbox Code Playgroud)
然后在程序中我有这样的事情:
template<class R>
R Calculator::expr_()const
{ …Run Code Online (Sandbox Code Playgroud) //file list.h
#include "stdafx.h"
namespace st
{
struct My_List;
typedef My_List list;
list* create(const char* name);
}
//file list.cpp
#include "stdafx.h"
#include "list.h"
namespace st
{
struct My_List
{
const char* name_;
My_List* left_;
My_List* right_;
My_List(const char* name):name_(name),
left_(nullptr),
right_(nullptr)
{}
My_List(const My_List&);
~My_List()
{
}
void insert(My_List*);
void set_name(char* name)
{
name_ = name;
}
const char* get_name()const
{
return name_;
}
};
typedef My_List list;
/*helper class for optor+ */
struct MyChar
{
const char* my_data_;
MyChar(const …Run Code Online (Sandbox Code Playgroud) 我正在使用VS2010 Ultimate.有代码:
//file IntSet.h
#include "stdafx.h"
#pragma once
/*Class representing set of integers*/
template<class T>
class IntSet
{
private:
T** myData_;
std::size_t mySize_;
std::size_t myIndex_;
public:
#pragma region ctor/dtor
explicit IntSet();
virtual ~IntSet();
#pragma endregion
#pragma region publicInterface
IntSet makeUnion(const IntSet&)const;
IntSet makeIntersection(const IntSet&)const;
IntSet makeSymmetricDifference(const IntSet&)const;
void insert(const T&);
#pragma endregion
};
//file IntSet_impl.h
#include "StdAfx.h"
#include "IntSet.h"
#pragma region ctor/dtor
template<class T>
IntSet<T>::IntSet():myData_(nullptr),
mySize_(0),
myIndex_(0)
{
}
template<class T>
IntSet<T>::~IntSet()
{
}
#pragma endregion
#pragma region publicInterface
template<class …Run Code Online (Sandbox Code Playgroud)