小编The*_* do的帖子

如何只删除一次指针?

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_?

c++

1
推荐指数
1
解决办法
122
查看次数

谁是对的,谁是错的又名GCC vs Visual Studio

此代码(不能"正常"工作)但在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)

c++ gcc visual-studio-2010 visual-c++

1
推荐指数
1
解决办法
1314
查看次数

类型特征不起作用

这个:

cout << std::is_const<const int*>::value; 
Run Code Online (Sandbox Code Playgroud)

打印错误,我认为它应该打印真实.为什么打印错误?

c++ metaprogramming

1
推荐指数
1
解决办法
178
查看次数

布尔方程

为什么这段代码有两个不同的输出(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)

c++ boolean-logic

1
推荐指数
1
解决办法
353
查看次数

不能专门化struct

为什么这不起作用?

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)

c++ template-specialization

1
推荐指数
1
解决办法
74
查看次数

BOOST_CHECK中的错误?

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与目标不同.

c++ testing boost

1
推荐指数
1
解决办法
250
查看次数

减少算法的时间复杂度

这是从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

我们可以在四个地方拆分这个磁带:

    P = 1, difference = |3 ? 10| = 7
    P = …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm

1
推荐指数
1
解决办法
884
查看次数

隐式调用operator [C++]

我定义了两个类:

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)

c++ operator-overloading

0
推荐指数
1
解决办法
465
查看次数

使用未定义的类型

//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)

c++

0
推荐指数
1
解决办法
408
查看次数

关于VS2010 C++中断点的问题

我正在使用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)

c++ visual-studio-2010

0
推荐指数
1
解决办法
828
查看次数