小编4pi*_*ie0的帖子

operator new已经有了一个正文

我正在尝试使用参数作为全局实现operator new.如果没有args的new被重载没有问题,但是在尝试编译时我遇到了以下错误

inline void* operator new(size_t, void* p) {
    //...
    return p;
}
Run Code Online (Sandbox Code Playgroud)

c:\ bjarne_exercise_6.cpp(14):错误C2084:函数'void*operator new(size_t,void*)throw()'已经有一个正文c:\ program files\microsoft visual studio 10.0\vc\include \new( 55):看看'new'的先前定义

c:\ bjarne_exercise_6.cpp(40):错误C2264:'operator new':函数定义或声明中的错误; 功能未被调用

我刚刚解决了这个问题,你必须在#include stdafx.h之前声明这个.不,不是真的.它编译得很好,但仍然没有调用此函数,但是来自新头文件的版本.是这样,因为新标题中已经定义了新的(有2个参数).普通的new(只有1,size_t参数)只在那里声明,所以你仍然可以重载它.因此,如果你想要带有多个参数的特殊new,下面的@trion建议的解决方案是合适的.

c++ operators new-operator

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

根据条件拆分 STL 列表

我有一个 std::list ,如下所示(x 标记表示小于 500 的数字)

x,x,x,x,503,x,x,x,510,x,x,x,502,x,x,x,x,x,x,600 - std::list<int> originallist
Run Code Online (Sandbox Code Playgroud)

我希望将列表拆分为列表向量,std::vector<std::list<int> >如下所示

1st element of vector: x,x,x,x,503
2nd element of vector: x,x,x,510
...
...
last element of vector: x,x,x,x,x,x,600
Run Code Online (Sandbox Code Playgroud)

我现在的代码如下:

list<int> templist; vector<list<int> > v;
for(list<int>::iterator lit=originallist.begin(); lit!=oriniallist.end(); ++lit) {
    if (*lit > 500) {
        templist.push_back(*lit);v.push_back(templist); templist.clear(); continue;
    }
    templist.push_back(*lit);
}
Run Code Online (Sandbox Code Playgroud)

在 C++ 中不使用 templist 实现上述任务的最有效方法是什么?任何帮助表示赞赏。

c++ algorithm stl list vector

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

istream_iterator eos

如果您将输入编码为

std::istream_iterator<std::string> ii(std::cin);
std::istream_iterator<std::string> eos;
std::for_each(ii,eos,record);
Run Code Online (Sandbox Code Playgroud)

我想知道您是否可以在控制台中放置一些东西,如果以这种方式编码,它将终止输入过程。

c++ iterator cin

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

速度参考值与值

我有以下问题.我尝试按值和引用进行分配,并且如此处所述,分配ba值应该更快,尽管当我尝试我的代码时它给了我一个相当混合的结果,因为有时assign1更快,有时assign2.

    class MyAddress{
    char *name;
    long int number;
    char *street;
    char *town;
    char state[2];
    long zip;
    std::vector<int> v_int;
public:
    MyAddress(int i){
        v_int.resize(1000000);
        std::fill(v_int.begin(),v_int.end(),i);
    }
    MyAddress& assign1(MyAddress const& x)
    { 
        MyAddress tmp(x);          // copy construction of tmp does the hard work
        std::swap(*this, tmp);  // trade our resources for tmp's
        return *this;      // our (old) resources get destroyed with tmp 
    }
    MyAddress& assign2(MyAddress x)//x is a copy of the source; hard work already done
    { 
        std::swap(*this, x);  // trade our …
Run Code Online (Sandbox Code Playgroud)

c++ reference pass-by-reference

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

QSharedPointer :: isNull()和operator!()之间的区别

在Qt文档中我们读到:

bool QSharedPointer::operator! () const

Returns true if this object is null. 
This function is suitable for use in if-constructs, like:

 if (!sharedptr) { ... }
Run Code Online (Sandbox Code Playgroud)

bool QSharedPointer::isNull () const
Returns true if this object is holding a reference to a null pointer.
Run Code Online (Sandbox Code Playgroud)

这两个功能有什么区别?这很清楚什么是对空指针的引用,但这意味着什么

"如果对象为空"?

什么决定是否QSharedPointer 为空?这些功能如何对应QSharedPointer::data() != null

c++ qt shared-ptr qsharedpointer

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

QFile:如何有效地只读取从k到k + L的字节

我可以从QFile中读取从k到k + L的字节,将第一个整个文件读入QByteArray

if (!file.open(QIODevice::ReadOnly))
    //...
    QByteArray blob = file.readAll();
    QByteArray bytes = blob.mid( k, L);
Run Code Online (Sandbox Code Playgroud)

如何有效地从k到k + L只读取字节?

if (!file.open(QIODevice::ReadOnly))
    //...
    QByteArray bytes = bytesFromFile( file, k, L);
Run Code Online (Sandbox Code Playgroud)

c++ qt bytearray qfile qbytearray

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

在同一程序中调用srand()两次

为什么当我在2个非常不同的点上调用srand()时它会导致数字不随机?一旦我删除其中一个它恢复正常.

c++ random srand visual-c++

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

确定平方根是否为整数

在我的程序中,我试图找到数字600851475143的最大素数因子.我已经制作了一个for循环,它确定了该数字的所有因子并将它们存储在向量数组中.我遇到的问题是我不知道如何确定因子是否可以是平方根并且给出整数而不是小数.到目前为止我的代码是:

#include <iostream>
#include <vector>
#include <math.h>

using namespace std;
vector <int> factors;

int main()
{
    double num = 600851475143;
    for (int i=1; i<=num; i++)
    {
        if (fmod(num,i)==0)
        {
            factors.push_back(i);
        }
    }

     for (int i=0; i<factors.size(); i++)
     {
         if (sqrt(factor[i]))                      // ??? 
     }
}
Run Code Online (Sandbox Code Playgroud)

有人可以通过我的if语句告诉我如何确定一个数字是否可以平方根?

c++ arrays math loops sqrt

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

使用char**,将char**初始化为C中的字符串数组

char *c[] = { "str1", "str2", "str3", "str4" }; 
char **c = { "str1", "str2", "str3", "str4" }; 
Run Code Online (Sandbox Code Playgroud)

第一行有效.第二个不是.为什么?

c string pointers initialization char

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

在一个范围内随机,是否为新的rand()版本存在数字偏差?

从各种其他SO问题中读取,当使用rand()%N时,您可能会修改您获得的伪数的偏差,因此您通常必须引入一些范围处理.

但是在所有情况下总是提到rand(),而不是较新的random()或arcrandom4()函数或本机C++ 11方法.当你在一组上运行这些例程时会发生什么?你有像rand()那样的偏见吗?

谢谢.

c++ random math distribution modulo

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