我正在尝试使用迭代器打印出一组中的每个成员.据我所知,从其他stackoverflow答案,我有正确的格式.当我运行此代码时,它正确输出myset的大小为3,但它只输出ii一次.如果我用*iter取消注释该行,Visual Studio会抛出一个运行时异常,说"map/set iterator不是dereferencable.任何想法为什么?
int main()
{
set<int> myset;
myset.insert(5);
myset.insert(6);
myset.insert(7);
set<int>::iterator iter;
cout<<myset.size()<<endl;
int ii=0;
for(iter=myset.begin(); iter!=myset.end();++iter);{
//cout<<(*iter)<<endl;
ii+=1;
cout<<ii<<endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我想创建一个数据向量,但我想设置它的大小并在子函数中填充它的元素.这是使用新运营商的合适时机吗?有没有更好的方法呢?这似乎是一个合适的时间,但我犹豫不决,因为为什么C++程序员应该尽量减少"新"的使用?
int main()
{
vector<double> *array1;
vector<double> *array2;
OtherArgs otherArgs;
FillArrays(array1,array2,otherArgs);
//Do other stuff
delete array1;
delete array2;
}
void FillArrays(vector<double> *&array1, vector<double> *&array2, OtherArgs &otherArgs)
{
int size=GetSize(otherArgs);
array1 = new vector<double>(size);
array2 = new vector<double>(size);
//Other code to fill the arrays
}
Run Code Online (Sandbox Code Playgroud)
谢谢