1388 字
7 分钟
我是如何因为错误理解C++数组初始化而空耗2小时的
正确的用法
整数型与浮点型
整数型的操作与浮点型相差不大,以下全部用整数型演示
如果你只想要将数组初始化为0
无论是不是在主函数外,你都可以:
// 方式1:使用大括号(推荐)int A[30][30] = {0};
// 方式2:使用空大括号(C++11起)int B[30][30] = {};
// 方式3:使用嵌套大括号int C[30][30] = {{0}};
// 方式4:值初始化(C++11)int D[30][30]{};在主函数外时,单单一个 int E[30][30]; 也可以起到相同作用:
#include <iostream>using namespace std;int E[30][30];int main (){ for (int i = 0; i < 30; i++){ for (int j = 0; j < 30; j++){ cout << E[i][j] << endl; } } return 0;}如果你想把数组初始化为某个非0的数
使用std::fill(最推荐):
#include <iostream>#include <algorithm> // 需要包含这个头文件using namespace std;int main() { int A[30][30]; fill(&A[0][0], &A[0][0] + 30 * 30, 7); // 初始化为7 // 验证 cout << A[0][0] << endl; //里面的数字只要不越界随便填 return 0;}也可以这样:
#include <iostream>#include <algorithm>using namespace std;int main() { int A[30][30]; fill(A[0], A[0] + 30 * 30,7); // 初始化为7 // 验证 cout << A[14][5] << endl; return 0;}还可以这样用(这样需要一维数组):
#include <iostream>#include <algorithm>using namespace std;int main() { int A[30]; fill(A, A + 30, 7); // 初始化为7 // 验证 cout << A[5] << endl; return 0;}嵌套循环(最易懂):
#include <iostream>using namespace std;int main() { int A[30][30]; for (int i = 0; i < 30; i++) { for (int j = 0; j < 30; j++) { A[i][j] = 5; // 初始化为5 } } // 验证 cout << A[29][29] << endl; return 0;}memset(只能为0或-1):
#include <iostream>#include <cstring>using namespace std;int main() { int A[30][30]; // 设置为-1(所有字节都是0xFF) memset(A, -1, sizeof(A)); // 如果用memset设置非0非-1的值,例如 // memset(A, 1, sizeof(A)); // 每个int会变成0x01010101,不是1 // 验证 cout << A[14][5] << endl; return 0;}如果你想手动指定某个位置为某个数
不怕累的话就一个个打吧:
#include <iostream>using namespace std;int main() { int A[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} }; // 验证 cout << A[1][1] << endl; return 0;}字符串型
如果你想把数组初始化化为空
无论是不是在主函数外,你都可以:
// C风格字符数组的不同初始化方式char S1[30][30] = {0}; // 全部初始化为'\0'char S2[30][30] = {}; // C++11: 全部初始化为'\0'
// std::string数组的不同初始化方式string strS1[30][30]; // 即使是局部声明string也能正确初始化string strS2[30][30] = {}; // 显式初始化为空字符串在主函数外时, char S3[30][30]; 也可行的:
#include <iostream>using namespace std;char S3[30][30];int main (){ // 验证 cout << S3[10][10] return 0;}如果你想把数组初始化为某个字符
对于char可以:
#include <iostream>#include <algorithm> //std::fill需要#include <cstring> //memset需要using namespace std;int main() { char chars[30][30]; // 方法1:使用fill fill(&chars[0][0], &chars[0][0] + 30 * 30, 'A'); // 方法2:使用memset memset(chars, 'Y', sizeof(chars)); // 方法3:使用循环 for (int i = 0; i < 30; i++) { for (int j = 0; j < 30; j++) { chars[i][j] = 'X'; } } // 验证 cout << chars[0][0] << endl; return 0;}string可以直接初始化为字符串,当然这个字符串可以为一个字符
如果你想把数组初始化为某个字符串
#include <iostream>#include <string>#include <algorithm>using namespace std;int main() { string strings[30][30]; // 方法1:使用循环 for (int i = 0; i < 30; i++) { for (int j = 0; j < 30; j++) { strings[i][j] = "default"; // 初始化为"default" } } // 方法2:使用fill(C++11及以上) fill(&strings[0][0], &strings[0][0] + 30 * 30, string("initial")); // 验证 cout << "strings[0][0] = " << strings[0][0] << endl; return 0;}我之前错误的理解
众所周知, int A[30][30] = {0}; 的作用是将数组初始化为0,然而当时的我没有深究背后的原理
以为{0}里面的数就是初始化对应的值,改成{1},就是初始化为1
然而,这只能将其第一个数设为1,而后面的数由于没有设置则都为1
一道简单的贪心题(虽然思路不止贪心)就因为这个小问题卡了2小时
错误代码:
#include <iostream>using namespace std;int main (){ int n; cin >> n; int a[1010][1010] = {1}; int ans = 0; int count = -1; int sum = 0; int resp = 0; for (int i = 0; i < n; i++){ for (int j = 0; j < n; j++){ cin >> a[i][j]; } } if (a[0][0] == 1){ cout << ans; return 0; } for (int i = 0; i <= n; i++){ for (int j = 0; j <= n; j++){ if (a[i][j] == 1){ if (count == j){ sum += j; } else if (count == -1){ count = j; sum = j; } else { resp = max(resp,sum); sum = j * (i + 1); count = j; } break; } } } ans = resp; cout << ans; return 0;}修正后的:
#include <iostream>using namespace std;int main (){ int n; cin >> n; int a[n + 10][n + 10] = {1}; for (int i = 0; i <= n; i++){ a[i][n] = 1; a[n][i] = 1; } int ans = 0; int count = -1; int sum = 0; int resp = 0; for (int i = 0; i < n; i++){ for (int j = 0; j < n; j++){ cin >> a[i][j]; } } if (a[0][0] == 1){ cout << ans; return 0; } for (int i = 0; i <= n; i++){ for (int j = 0; j <= n; j++){ if (a[i][j] == 1){ if (count == j){ sum += j; } else if (count == -1){ count = j; sum = j; } else { resp = max(resp,sum); sum = j * (i + 1); count = j; } for (int x = i; x <= n; x++){ for (int y = j; y <= n; y++){ a[x][y] = 1; } } break; } } } ans = resp; cout << ans; return 0;} 我是如何因为错误理解C++数组初始化而空耗2小时的
https://shuntianyifang.github.io/shuntian-blog/posts/post3/我是如何因为错误理解c数组初始化而空耗2小时的/