小编Ram*_*ama的帖子

if(intVar)和if(intVar!= 0)之间有区别吗?

我很想知道这两个if块在c ++中是否存在差异.如果得到答案你会引用一些参考,这将是非常有用的.

if ( intVar!= 0 )
{
  //Do something
}
Run Code Online (Sandbox Code Playgroud)

if (intVar)
{
  //Do samething
}
Run Code Online (Sandbox Code Playgroud)

其中intVar,可以是任何类型的具有任何值的整数变量.

[编辑]关于主题"重复的问题".我没有发现任何涉及if语句的问题.

c++

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

为什么析构函数不释放数组内存?

我的问题是为什么析构函数不释放我的临时数组的内存?Valgrind告诉我,我在构造函数中使用了new运算符,但之后没有删除内存.当我简单地写作时delete temp,我在Valgrind中遇到了许多错误,例如无效读取大小,双重免费等等.你能告诉我们这里发生了什么吗?

array_xyz(const int r, const int c, double **arg_array) {

    rows = r;
    cols = c;
    array_xyz *temp = new array_xyz();
    temp->arr = new double *[rows];
    temp->rows = r;
    temp->cols = c;
    arr = new double *[rows];

    for (int i = 0; i < rows; i++) {
        arr[i] = new double [cols];
        temp->arr[i] = new double [cols];
    }

    for (int j = 0; j < rows; j++) {
        for (int k = 0; k < cols; k++) …
Run Code Online (Sandbox Code Playgroud)

c++ valgrind destructor delete-operator

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

如果我有一个向量(或任何类似的)成员变量,移动构造函数如何?

标题几乎总结了我的问题.更详细:我知道当我在C++ 11中声明移动构造函数和移动赋值运算符时,我必须"使其他对象变量为零".但是,当我的变量不是一个array或一个简单intdouble值,但它是一个更"复杂"的类型时,它是如何工作的?

在这个例子中,我有一个Shoplist带有vector成员变量的类.我是否必须vector在移动赋值运算符和构造函数中调用类的析构函数?或者是什么?

class Shoplist {
public:
    Shoplist() :slist(0) {};
    Shoplist(const Shoplist& other) :slist(other.slist) {};
    Shoplist(Shoplist&& other) :slist(0) {
        slist = other.slist;
        other.slist.~vector();
    }

    Shoplist& operator=(const Shoplist& other);
    Shoplist& operator=(Shoplist&& other);



    ~Shoplist() {};
private:
    vector<Item> slist;
};

Shoplist& Shoplist::operator=(const Shoplist& other)
{
    slist = other.slist;
    return *this;
}

Shoplist& Shoplist::operator=(Shoplist&& other)
{
    slist = other.slist;
    other.slist.~vector();
    return *this;
}
Run Code Online (Sandbox Code Playgroud)

c++ move-semantics c++11

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

从方法非确定性行为返回std :: string?

我有一个读取文件并将其内容作为a返回的方法std::string.我使用return std::string来编译OpenGL程序.在某些情况下,由于一个或两个着色器部分(读取文件内容),链接失败(编译的顶点着色器已损坏.)NULL.

  • 如果我一步一步调试我的代码一切都很好.
  • 如果我打印文件内容,链接似乎失败的次数较少.

为什么它表现不同,哪里是我的错?

cout在年底始终打印正确的文件内容:

std::string read_file(const char* filePath) {
    std::string content;
    std::ifstream stream(filePath, std::ios::in);
    if (stream.is_open()) {
        std::string line = "";
        while(getline(stream, line)) {
            content += "\n" + line;
        }
    }
    std::cout << "read_file " << content << "end read_file" << std::endl; // always prints the correct content

    return content; // makes a copy, don't like that, unless RVO?..
}
Run Code Online (Sandbox Code Playgroud)

fragmentSource和或vertextSource有时是空的:

    GLuint fragmentShaderID = glCreateShader(GL_FRAGMENT_SHADER);
    GLuint vertexShaderID …
Run Code Online (Sandbox Code Playgroud)

c++ opengl

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

从const char*参数中取出索引

我有以下代码:

int some_array[256] = { ... };

int do_stuff(const char* str)
{
   int index = *str;
   return some_array[index];
}
Run Code Online (Sandbox Code Playgroud)

显然上面的代码会导致某些平台出现错误,因为*str实际上可能是负面的.

所以我想到了两个可能的解决方案:

  1. 在赋值(unsigned int index = (unsigned char)*str;)上转换值.

  2. 传递const unsigned char*代替.

编辑:这个问题的其余部分没有得到治疗,所以我把它移到一个新的线程.

c c++ string

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

使用大型数据集初始化类成员向量的最有效方法

使用数据初始化std::vector类中的成员变量的最快/内存有效方法是什么?

这是我能够提出的:

class LargeClass
{
//lot of data, with long running constructor to initialize them 
}

class EngineClass
{

public:
EngineClass( const vector<LargeClass>& vectorOfLargeClass )
{
mVectorOfLargeClass = vectorOfLargeClass; //Here is what I was able to come up
}

private:
vector<LargeClass> mVectorOfLargeClass;

}

int main()
{
vector<LargeClass> vectorOfLargeClass;
...
//fill vectorOfLargeClass with a lot of data
...
EngineClass engine( vectorOfLargeClass );
...
}
Run Code Online (Sandbox Code Playgroud)

我需要EngineClass保存vectorOfLargeClass在中创建的矢量数据的副本main().

我使用C++ 11,但我并不完全熟悉这种语言,也许有更好的标准方法来完成这样的任务.

c++ c++11

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

使用std :: map和std :: list :: iterator

为什么这段代码不能编译?

#include <map>
#include <list>

int main()
{
    typedef std::list<int>::iterator Iter;

    std::map<Iter, Iter> m;

    std::list<int> ints;

    m[ints.begin()] = ints.begin();
}
Run Code Online (Sandbox Code Playgroud)

如果我改变std::liststd::vector,一切正常.

错误日志如下:

|| "==== Building Practical (release) ===="
|| main.cpp
In file included from C:\mingw32\i686-w64-mingw32\include\c++\bits\stl_tree.h|65| 0,
||                  from C:/mingw32/i686-w64-mingw32/include/c++/map:60,
||                  from ../src/main.cpp:1:
|| C:/mingw32/i686-w64-mingw32/include/c++/bits/stl_function.h: In instantiation of 'bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = std::_List_iterator<int>]':
C:\mingw32\i686-w64-mingw32\include\c++\bits\stl_map.h|498 col 32| required from 'std::map<_Key, _Tp, _Compare, _Alloc>::mapped_type& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](std::map<_Key, _Tp, _Compare, _Alloc>::key_type&&) [with _Key = …
Run Code Online (Sandbox Code Playgroud)

c++ iterator compiler-errors std standard-library

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

在C++中从虚拟析构函数调用虚方法

我想破坏一个类的对象B.

class A {
public:
    A() {
        std::cout << "construct A" << av::endl;
        a = new int;
    }
    virtual ~A() {
        std::cout << "destruct A" << av::endl;
        this->clear();
    }
    virtual void clear() {
        std::cout << "clear A" << av::endl;
        delete a;
    }
protected:
    int *a;
};

class B : public A {
public:
    B() {
        std::cout << "construct B" << av::endl;
        b = new int;
    }
    ~B() {
        std::cout << "destruct B" << av::endl;
    }
    void clear() override …
Run Code Online (Sandbox Code Playgroud)

c++ destructor memory-leaks memory-management

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

UTF8 数据到 std::string 或 std::wstring

我从 HTTP 服务器响应接收正文字节,但我不知道如何将它们转换为 UTF8 字符串以使用它们。

我有一个想法,但我不确定它是否有效。我需要获取响应的字节并搜索它们并修改它们,因此我需要将std::vector<BYTE>tostd::wstringstd::string.

响应的 UTF8 字节编码在 my 中std::vector<BYTE>,如何将它们转换为std::string?我要不要把它们转换成std::wstring?。

我找到了这个代码:

std::string Encoding::StringToUtf8(const std::string& str)
{
INT size = MultiByteToWideChar(CP_ACP, MB_COMPOSITE, str.c_str(), str.length(), NULL, 0);

std::wstring utf16_str(size, '\0');

MultiByteToWideChar(CP_ACP, MB_COMPOSITE, str.c_str(), str.length(), &utf16_str[0], size);

INT utf8_size = WideCharToMultiByte(CP_UTF8, 0, utf16_str.c_str(), utf16_str.length(), NULL, 0, NULL, NULL);

std::string utf8_str(utf8_size, '\0');

WideCharToMultiByte(CP_UTF8, 0, utf16_str.c_str(), utf16_str.length(), &utf8_str[0], utf8_size, NULL, NULL);

return utf8_str;
Run Code Online (Sandbox Code Playgroud)

}

但是现在,如果我想在字符串中搜索像“Ñ”这样的字符会起作用吗?,或者我是否要转换 a 中的字节std::wstring并搜索“Ñ”修改std::wstring并将其转换为std::string …

c++ string encoding std wstring

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

如何仅在c ++中比较其他字段对?

我有比较对的这个问题.我想用到find()包含这样的对的vector:

vector<pair<PersonID, Cost>> friendlist;
Run Code Online (Sandbox Code Playgroud)

PersonID并且Cost都是常规的整数.而这里的问题是我想专门使用find()刚才的PersonID,我对此并不感兴趣Cost.

if(friendlist.begin(), friendlist.end(), std::make_pair(toid, Cost)) != 
friendlist.end() )
Run Code Online (Sandbox Code Playgroud)

toid这是我想在向量中寻找的id.那么我应该对make_pair需要的其他领域做些什么呢?

c++

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