小编hiv*_*ert的帖子

C++ std :: set comparator

这是代码:

struct comp
{
    bool operator()(Reputation *one, Reputation *two)
    {
        if (one->Amount < 0 && two->Amount >= 0)
            return false;
        if (one->Amount >= 0 && two->Amount < 0)
            return true;

        if (one->Amount >= 0)
            return one->Amount <= two->Amount;
        else
            return one->Amount >= two->Amount;
    }
};
Run Code Online (Sandbox Code Playgroud)

这就是问题所在:

调试断言失败!
文件:..\VC\include\xtree
行:638

表达式:无效的运算符<

之后,我可以选择"Abort","Retry"或"Ignore".如果我选择忽略更多(相同的),但它最终会完美地工作.

当我将 - > Amount ==的信誉插入到之前插入的信誉*之一时,似乎会出现问题,但我不确定最后一个.

任何帮助将不胜感激

编辑:我希望他们订购的顺序首先是按顺序排列的顺序,然后按顺序顺序排列.示例:1 5 10 11 11 20 50 -1 -5 -50

c++ set comparator stdset

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

constexpr与operator | =

我尝试编写一个函数,它使用C++ 0x constexpr返回一个只有输入集最高位的整数.

constexpr inline uint64_t
get_highest_bit(uint64_t p)
{
  return 
     (p|=(p>>1)),
     (p|=(p>>2)),
     (p|=(p>>4)),
     (p|=(p>>8)),
     (p|=(p>>16)),
     (p|=(p>>32)),
     (p-(p>>1));
}
Run Code Online (Sandbox Code Playgroud)

这使用gcc 4.6.1导致编译时失败.

error: expression ‘(p <unknown operator> ((p >> 1) | p))’ is not a constant-expression
Run Code Online (Sandbox Code Playgroud)

请注意,它没有constexpr关键字.

我的问题是:

为什么这不起作用?我可以看到运算符| =不是constexpr,但对于内置类型是否重要?

有没有一种简单的方法可以将此函数写为constexpr?我希望它在运行时合理有效,我关心可读性.

constexpr c++11

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

Variadic模板无法识别constexpr功能

我正在尝试在编译时初始化一些C++数组但是我得到了一个奇怪的g ++错误.这是我能够获得的最小代码块,它可以重现错误:

#include <array>

template<typename Ar, int... Vals>
constexpr Ar Map(typename Ar::value_type /*int*/ fun(int)) 
{ return {{ fun(Vals)... }}; }

constexpr int add(int i) { return i + 1; }

constexpr auto b = Map<std::array<int, 2>, 1, 2>(add);
Run Code Online (Sandbox Code Playgroud)

编译器在抱怨

bug.cpp:8:53:   in constexpr expansion of ‘Map<std::array<int, 2ul>, {1, 2}>(add)’
bug.cpp:4:80: error: expression ‘add’ does not designate a constexpr function
 constexpr Ar Map(typename Ar::value_type /*int*/ fun(int)) { return {{ fun(Vals)... }}; }
Run Code Online (Sandbox Code Playgroud)

这与g ++ 4.7.1和4.9.0 20130520(实验)一起发生.需要注意的是,如果我取代typename Ar::value_type通过int(见注释)中的定义 Map …

c++ type-safety variadic-templates constexpr c++11

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

大型内存大小分配给int类型的缓冲区

我正在做一个visual c ++应用程序并尝试将大小分配给缓冲区(该缓冲区进一步用于存储流的内容).如果声明缓冲的大小小于那么没有问题

const int size= 319000; //here there is no problem
Run Code Online (Sandbox Code Playgroud)

但是为了从流中访问我想要的一些数据,我需要声明这样大小的缓冲区 -

const int size=4348928;//this size cause the problem
char buffer[size+1];
HRESULT hr = pStream->Read(buffer, size, &cbRead );
Run Code Online (Sandbox Code Playgroud)

虽然代码的最后两行对我的问题没有任何作用,但它只是让你知道我正在用这个缓冲区的大小做什么.

但是,当我声明这个大小时,它什么也没做(我的意思是我的视觉应用程序功能如下:如果你单击一个文件,它会生成一个流,我将该流存储在缓冲区中 - 如果我声明大小为319000的顺序程序运行正常,当大小增加到4348928它甚至不工作 - 当然没有错误)

c++ buffer types buffer-overflow

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

什么是Scala的seq.reduceOption的Clojure等价物?

在Scala的Collections库中 - 我们看到具有reduceOption方法的Seq类:

def reduceOption[A1 >: A](op: (A1, A1) ? A1): Option[A1]
Run Code Online (Sandbox Code Playgroud)

使用指定的关联二元运算符减少此序列的元素(如果有)."

这有另一个很好的解释:

这里谈到reduceOption保存一天!如果reduce因为集合是空会失败,它只会返回一个None,而是和一个Some(returnvalue)用于非空的集合.

我的问题是:Scala的seq.reduceOption的Clojure相当于什么?

reduce scala clojure

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

使用带有c ++中的set的begin()和end()

我正在尝试使用迭代器来完成一个集合,然后对该集合的成员做一些事情(如果有的话).问题在于,通常这是有效的,但有时,它会比较空集的开头和结尾,并发现它们不相等.

感兴趣的代码段是:

    for(int i=0;i<input_data.num_particles();i++)
    {
        //loop through pairs contained in particle i's Verlet list
        set<int>::iterator iter;
        for(iter=verlet_vars.verlet()[i].begin();iter!=verlet_vars.verlet()[i].end();iter++)
        {
            //call the force() function to calculate the force between the particles
            force(particles.getpart(i),particles.getpart(*iter),input_data,*iter);  
        }
    }
Run Code Online (Sandbox Code Playgroud)

有时,即使verlet_vars.verlet()[i]中包含的集合为空,程序也会将迭代器与集合的末尾进行比较并发现它们不相等,因此它进入内部循环(最终导致程序崩溃)通过尝试调用force()函数).奇怪的是,如果我在调用内部循环之前对迭代器做了什么,比如做类似的事情:

iter=verlet_vars.verlet()[i].begin();
Run Code Online (Sandbox Code Playgroud)

然后,内部循环的比较总是返回true,程序正常运行.

PS命令verlet_vars.verlet()[i]调用集合的向量,因此[i]

verlet()函数:

std::vector<std::set<int> > verlet() const {return _verlet;}
Run Code Online (Sandbox Code Playgroud)

谢谢你的时间.

c++ set stdvector stdset

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

如何乘以2个大数?

我创建了一个小的控制台应用程序,乘以2长整数.我不知道我的问题在哪里.这个应用程序工作正常,直到位数为3.

但如果数字的位数大于3,则应用程序的输出错误.:(

请告诉我我的问题在哪里,我解决了.

这是我的代码:

int digits (int n)
{
    int counter = 0;
    while (n > 0)
    {
        n/=10;
        counter++;
    }
    return counter;
}

long longMultiply(long a, long b)
{
    const int S = 3;
    int w,x,y,z;
    int n = max(digits(a),digits(b));

    if(a == 0 || b ==0) {
        return 0;
    } else if (n <= S) {
        return a*b;
    } else {
        int m = (n/2);

        //first number
        x = a/(10^m);
        y = a%(10^m);

        //second number
        w = b/(10^m);
        z …
Run Code Online (Sandbox Code Playgroud)

c++ algorithm math

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

升压测试无法找到自定义打印

由于某种原因boost::test无法编译以下代码

#define BOOST_TEST_MODULE EPUTests

#include <iostream>
#include <boost/test/unit_test.hpp>

using epu8 = uint8_t __attribute__((vector_size(16)));

std::ostream &operator<<(std::ostream &stream, epu8 const &term) {
    stream << "[" << unsigned(term[0]);
    for (unsigned i = 1; i < 16; ++i)
        stream << "," << unsigned(term[i]);
    stream << "]";
    return stream;
}

bool failtest(epu8 x) { return false; }


//****************************************************************************//
BOOST_AUTO_TEST_SUITE(EPU8_test)

BOOST_AUTO_TEST_CASE(EPU8_equal) {
    epu8 x {};
    BOOST_CHECK_PREDICATE(failtest, (x));
}

BOOST_AUTO_TEST_SUITE_END()
//****************************************************************************//
Run Code Online (Sandbox Code Playgroud)

错误信息是

In file included from /usr/include/boost/test/tools/floating_point_comparison.hpp:21:0,
             from /usr/include/boost/test/tools/old/impl.hpp:21,
             from /usr/include/boost/test/test_tools.hpp:46,
             from /usr/include/boost/test/unit_test.hpp:18,
             from boost_test_epu.cpp:4: …
Run Code Online (Sandbox Code Playgroud)

c++ printing boost-test

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

SAGE:有限域上的多项式系数

我需要一些帮助才能得到多项式的系数.如果试过

y = var('y')
q = y^3 -2*y + 1
coeff_list = [q(y=0)] + [q.coeff(y^k) for k in range(1, q.degree(y)+1)]
Run Code Online (Sandbox Code Playgroud)

但在GF(q)

S.<y> = PolynomialRing(GF(q),'y')  
q = y^3 -2*y + 1
coeff_list = [q(y=0)] + [q.coeff(y^k) for k in range(1, q.degree(y)+1)]
coeff_list
Run Code Online (Sandbox Code Playgroud)

我收到了这个错误

Error in lines 1-1
Traceback (most recent call last):
  File "/projects/31b0bdd7-734b-4864-bf87-0b7cfafd06e9/.sagemathcloud/sage_server.py", line 733, in execute
    exec compile(block+'\n', '', 'single') in namespace, locals
  File "", line 1, in <module>
  File "factory.pyx", line 141, in sage.structure.factory.UniqueFactory.__call__ (sage/structure/factory.c:1157)
  File "/usr/local/sage/sage-5.12/local/lib/python2.7/site-packages/sage/rings/finite_rings/constructor.py", line …
Run Code Online (Sandbox Code Playgroud)

polynomial-math sage

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

我不明白这段代码的输出

我正在学习C,我的老师给了我们一些功课.我们必须确定一些代码的输出.我不明白怎么做y=4.

代码如下

int main() {

    int w = 3, y = 3;
    printf("w = %i\n", --w + w--);
    printf("y = %i\n\n", y = w + y++);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c pre-increment post-increment

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

泛型类中的语法

我试图用模板解决一个练习.我的代码在大多数情况下运行良好,但我发现有一个案例无效,这是我的代码的一部分.默认比较器是<.

template <typename type1, typename typecomparator=less<typename type1::valuetype1> >
class Myclass
 {
   public:
   Myclass ( const type1 & arg1,const typecomparator & comparator = typecomparator () )
   {
    this->seq=arg1;
    this->comp=comparator;
   }
   ~Myclass ( void ){}
   // ...
   private:
    mutable type1 seq;
    typecomparator comp;
 };
Run Code Online (Sandbox Code Playgroud)

代码工作几乎是所有情况.为例:Myclass <string> test ( "abcabcabc" );

但是当我想要使用一个类时:

class another
 {
   public:
                   another ( bool caseSensitive )
                    : m_CaseSensitive ( caseSensitive ) { }
    bool           operator () ( const string & a, const string & b ) const …
Run Code Online (Sandbox Code Playgroud)

c++ templates comparator

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

C++数独解算器

需要帮助找出问题readPuzzleprintPuzzle功能有什么问题.SetOfSmallInts是包含{1,2,3,4,5,6,7,8,9}这些数字的任何组合的集合.singletonSet(s)在一组中存储单个数字.无论我在程序上运行什么输入,它只输出1.即使所有输入都是' - ',输出也是81 1.有什么建议?Puzzle中的Puzzle类型构成一个数组SetofSmallInts [9][9].

//==============================================================
//                      readPuzzle
//==============================================================
// Reads in puzzle p from the standard input.
//==============================================================

void readPuzzle(Puzzle p)
{
   int i, j;
   SetOfSmallInts s;
   s = rangeSet(1, 9);
   char n;

   for(i = 0; i < 9; i++)
   {
      for(j = 0; j < 9; j++) 
      {
       n = scanf("%c", &n);  
       if (n == '-')
       {
         p[i][j]= s;
       }
       else if(n==1 || n==2 || n==3 …
Run Code Online (Sandbox Code Playgroud)

c++ arrays multidimensional-array

-2
推荐指数
1
解决办法
314
查看次数