我想使用 2d std::array 因为我必须在程序中的某个点使用绑定检查。对于一维数组,我会这样做:
#include <iostream>
#include <array>
int main (){
std::array<int,4> myarray;
for (int i=0; i<10; i++){
myarray.at(i) = i+1;
}
}
Run Code Online (Sandbox Code Playgroud)
我如何为二维数组做到这一点。我可以在其中使用 auto 吗?
在以下代码中:
#include <iostream>
#include <assert.h>
int main()
{
int a,b;
cin>>a>>b;
char c,d;
cin>>c>>d;
assert ((a==b,a*b==9,c==d));
assert ( a==b && a*b==9 && c==d );
}
Run Code Online (Sandbox Code Playgroud)
如果你 :
第一个断言语句是弱的还是我对断言语句不了解?
我试图解决这个问题,我必须拼写数字。
当我尝试a通过引用调用我的字符串数组时,出现此错误。但是如果我按值调用它,我不会出错。
我不知道 rvalue 来自哪里,因为我的字符串元素应该被视为左值。
#include <iostream>
#include <string>
using namespace std;
void spell(int n,string* &a){
if(n==0)
return;
spell(n/10,a);
cout<<a[n%10];
}
int main(){
int n;
cin>>n;
string a[10]{"zero ","one ","two ","three ","four ","five ","six ","seven ","eight ","nine "};
spell(n,a);
if(n<0)
return 0;
return main();
}
Run Code Online (Sandbox Code Playgroud)