小编sho*_*ace的帖子

如何声明 2d std::array

我想使用 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 吗?

c++ stdarray

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

类似的断言语句给出不同的结果

在以下代码中:

#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)

如果你 :

  1. 输入不同的整数或不同的字符,第一个 assert 语句中止程序。(正如它应该)。
  2. 但是,如果您在此代码中输入相等的整数(3 以外)和相同的字符,则第一个 assert 语句不会中止程序,但是第二个 assert 语句会中止。

第一个断言语句是弱的还是我对断言语句不了解?

c++ assert comma-operator

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

类型 'std::string*&amp; {aka std::basic_string&lt;char&gt;*&amp;}' 的非const 引用从'std::string* {aka

我试图解决这个问题,我必须拼写数字。
当我尝试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)

c++ pass-by-reference

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