小编Mr.*_*C64的帖子

如何在C++中动态创建新的2D数组

如在主题中如何在C++中创建新的2D数组?下面的代码不能很好地工作.

int** t = new *int[3];
for(int i = 0; i < 3; i++)
       t[i] = new int[5];
Run Code Online (Sandbox Code Playgroud)

c++ dynamic multidimensional-array

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

为什么在写入char数组的边界时strcat_s会崩溃?

我知道,关于这个话题已有很多帖子,但我没有找到任何令人满意的内容.甚至谷歌也不是我的朋友.

所以,首先我的代码:

    void secureCat() {
        const int BUFFERSIZE = 5;
        char buffer[BUFFERSIZE];

        strcpy_s (buffer, BUFFERSIZE, "01");
        cout << "1, buffer=" << buffer << endl;

        errno_t rc = 0;

        // still works
        rc = strcat_s(buffer, BUFFERSIZE, "2"); 

        cout << (rc == 0 ? "yippee" : "oh noooo") << endl;
        cout << "2, buffer=" << buffer << endl;

        // and now the crashing line
        rc = strcat_s(buffer, BUFFERSIZE, "345"); 

        cout << (rc == 0 ? "yippee" : "oh noooo") << endl;
        cout …
Run Code Online (Sandbox Code Playgroud)

c++

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

为什么c_str()为两个不同的字符串返回相同的值?

给定一个简单的文件加载功能,

std::string load_file(const std::string &filename) {
    std::ifstream     file(filename);
    std::string       line;
    std::stringstream stream;
    while (std::getline(file, line)) {
        stream << line << "\n";
    }
    return stream.str();
}
Run Code Online (Sandbox Code Playgroud)

为什么以下打印another_file两次内容?

const char *some_file = load_file("some_file").c_str();
const char *another_file = load_file("another_file").c_str();
printf("%s", some_file);
printf("%s", another_file);
Run Code Online (Sandbox Code Playgroud)

c++ string

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

空指针指针(void **)

我正在http://msdn.microsoft.com/zh-cn/library/windows/desktop/dd389098(v=vs.85).aspx上阅读COM示例。

我真的无法理解(void **)

hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
Run Code Online (Sandbox Code Playgroud)

所以我尝试了由类的不同类型的指针返回的一些值

class Point{
private:
    int x, y;
public:
    Point(int inputX, int inputY){x = inputX, y = inputY;}
    int getX(){return x;}
    int getY(){return y;}
    friend ostream& operator << (ostream &out, Point &cPoint);
    Point operator-(){
        return Point(-x, -y);
    }
};

ostream& operator << (ostream &out, Point &cPoint){
    return out<< "(" << cPoint.x << ", " << cPoint.y << ")";
}
Run Code Online (Sandbox Code Playgroud)

然后打印出来

Point *p = new Point(1,2);
cout << p << endl << …
Run Code Online (Sandbox Code Playgroud)

c++ com pointers void-pointers pointer-to-pointer

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

C++将数组传递给函数

我对C++比较陌生,并且很难将我的数组传递给一个单独的函数.抱怨重新问一个毫无疑问已被回答十几次的问题,但我找不到任何与我的代码问题类似的问题.

int main()
{
    Array<int> intarray(10);
    int grow_size = 0;

    intarray[0] = 42;
    intarray[1] = 12;
    intarray[9] = 88;

    intarray.Resize(intarray.Size()+2);
    intarray.Insert(10, 6);

    addToArray(intarray);

    int i = intarray[0];

    for (i=0;i<intarray.Size();i++) 
    cout<<i<<'\t'<<intarray[i]<<endl;

    Sleep(5000);
}

void addToArray(Array<int> intarray)
{
    int newValue;
    int newIndex;

    cout<<"What do you want to add to the array?"<<endl;
    cin >> newValue;
    cout<<"At what point should this value be added?"<<endl;
    cin >> newIndex;

    intarray.Insert(newValue, newIndex);
}
Run Code Online (Sandbox Code Playgroud)

c++ arrays function parameter-passing

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

std :: vector内存分配问题

我在尝试删除指针向量时遇到问题:

std::vector<float*> *v;

v = new std::vector<float*>;
v->assign(2, new float[2]);

for (int i = 0; i < 2; ++i)
{
    delete[] v->at(i);
}
delete v;
Run Code Online (Sandbox Code Playgroud)

我正在删除整个向量中的每个元素,但我仍然得到一个断言.你能告诉我我做错了什么吗?

先感谢您.

c++ memory memory-management stl stdvector

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

移动参数或传递它们通常的C++ 98/03方式?

假设有一个Person这样的类:

// "string" is std::string, "move" is std::move

class Person
{
public:
    // C++11 way: pass by value and std::move() from the value
    Person(string name, string surname)
        : m_name(move(name)), m_surname(move(surname))
    {}

    ....

private:
    string m_name;
    string m_surname;
};
Run Code Online (Sandbox Code Playgroud)

假设我在某个循环中从某些源(例如文件)中读取namesurname值,并且在每次循环迭代中,我想创建Person具有这些读取值的值(例如,Person在某个容器中推送创建的).

Person通常的方式调用构造函数是否更好:

Person(name, surname)
Run Code Online (Sandbox Code Playgroud)

还是std::move用于参数?

Person(move(name), move(surname))
Run Code Online (Sandbox Code Playgroud)

c++ argument-passing move-semantics c++11

3
推荐指数
2
解决办法
206
查看次数

错误:运算符[]无匹配

我正在开发一个openFrameworks项目,我正在使用它vector来存储3d网格索引位置.但是,在尝试访问数据时,我得到:

error: no match for 'operator []' in ((icoSphere*)this)->icoSphere::subIndicies[i]
Run Code Online (Sandbox Code Playgroud)

数据类型是ofIndexType.

这是一些片段

icoSphere.h文件:

// vector created        
std::vector<ofIndexType> subIndicies;
Run Code Online (Sandbox Code Playgroud)

icoSphere.cpp文件:

// items added to vector
ofIndexType indA = mesh.getIndex(0);
ofIndexType indB = mesh.getIndex(1);
ofIndexType indC = mesh.getIndex(2);

subIndicies.push_back(indA);
subIndicies.push_back(indB);
subIndicies.push_back(indC);

// iterate through vector
for (std::vector<ofIndexType>::iterator i = subIndicies.begin(); i !=subIndicies.end(); i++)
{
    subMesh.addIndex(subIndicies[i]); // here is where the error occurs
}
Run Code Online (Sandbox Code Playgroud)

向量和迭代器都是ofIndexType(openFrameworks数据类型,本质上是无符号整数).无法理清为什么它说[]不是运营商.

c++ vector openframeworks

3
推荐指数
2
解决办法
1421
查看次数

为什么将unique_ptr与数组一起使用会导致编译错误?

为什么使用unique_ptr <string> items;而不是原始指针string *items;抛出编译错误.

#include <iostream>
#include <memory>
using namespace std;

class myarray
{
  private:
    unique_ptr <string> items;
    //string *items;
  public:
    myarray (int size=20) : items (new string [size] ) {}

    string& operator[] (const int index)
    {
      return items[index];
    }
};


int main()
{
  myarray m1(200);
  myarray m2;
  m1[19] = "test";
  cout << m1[19];
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

错误:

subscript2.cpp: In member function ‘std::string& myarray::operator[](int)’:
subscript2.cpp:15: error: no match for ‘operator[]’ in ‘((myarray*)this)->myarray::items[index]’
Run Code Online (Sandbox Code Playgroud)

c++ arrays unique-ptr

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

每个循环的 C++ 向量不会迭代引用?

昨天我花了差不多一个时间来调试这个东西,从那时起我就无法停止思考它。C

我尝试实现带有字符串索引的二维矩阵......

class CSqrMatrix(){
  ....
  void insert(string str){
     bool b = map.insert(str,m_size).second;
     if (b){
         m_size++;
         vector<int> ve;
         for (int i = 0; i < m_vect.size(); i++) 
             ve.push_back(m_default);
         m_vect.push_back(ve);
         for (auto v : m_vect)
             v.push_back(m_default);
     }
  ...
map<string,int> map;
vector < vector<int> > m_vect;
int m_default;
int m_size;
};
Run Code Online (Sandbox Code Playgroud)

经过一些插入后,当我尝试到达类似的元素时

m_vect[0][0] = 8;
Run Code Online (Sandbox Code Playgroud)

我遇到了无效的写入和段错误...并且 的值为m_vect[0].size()0;我尝试了一切,最后我将 for every 循环更改为普通循环,例如

for (int i = 0; i < m_vect.size(); i++){
     m_vect[i].push_back(m_default);
Run Code Online (Sandbox Code Playgroud)

该程序运行良好...

那么这是否意味着,这v不是引用,而是元素的新副本?

谢谢(代码可能有错别字,我是在手机上写的……)

c++ foreach reference vector pass-by-reference

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