如在主题中如何在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) 我知道,关于这个话题已有很多帖子,但我没有找到任何令人满意的内容.甚至谷歌也不是我的朋友.
所以,首先我的代码:
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) 给定一个简单的文件加载功能,
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) 我正在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++比较陌生,并且很难将我的数组传递给一个单独的函数.抱怨重新问一个毫无疑问已被回答十几次的问题,但我找不到任何与我的代码问题类似的问题.
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) 我在尝试删除指针向量时遇到问题:
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)
我正在删除整个向量中的每个元素,但我仍然得到一个断言.你能告诉我我做错了什么吗?
先感谢您.
假设有一个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)
假设我在某个循环中从某些源(例如文件)中读取name和surname值,并且在每次循环迭代中,我想创建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) 我正在开发一个openFrameworks项目,我正在使用它vector来存储3d网格索引位置.但是,在尝试访问数据时,我得到:
Run Code Online (Sandbox Code Playgroud)error: no match for 'operator []' in ((icoSphere*)this)->icoSphere::subIndicies[i]
数据类型是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数据类型,本质上是无符号整数).无法理清为什么它说[]不是运营商.
为什么使用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
我尝试实现带有字符串索引的二维矩阵......
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不是引用,而是元素的新副本?
谢谢(代码可能有错别字,我是在手机上写的……)