自己写的解答,如有错误之处,烦请在评论区指正!
buf_size
不是常量表达式;txt_size()
的返回值不是constexpr
;\0
。sa
:10 个空字符串;ia
:10 个 0;sa2
和ia2
中元素的值未定义。vector
特有的成员函数。ia
长度是 10,元素下标从 0 到 9。#include
int main() {
int a[10];
for (int i = 0; i != 10; ++i)
a[i] = i;
return 0;
}
#include
int main() {
int a[10];
for (int i = 0; i != 10; ++i)
a[i] = i;
int b[10];
for (int i = 0; i != 10; ++i)
b[i] = a[i];
return 0;
}
// vector 版本
#include
#include
using namespace std;
int main() {
vector<int> a(10, 0);
for (int i = 0; i != a.size(); ++i)
a[i] = i;
vector<int> b = a;
return 0;
}
scores
数组中元素的值都是未定义的,或者说是未知的,原本的那段内存空间上可以是任意值,于是统计就会出现错误。p1 = p2;
,一般都是合法的。#include
using namespace std;
int main() {
int a[100];
for (int *p = a; p != end(a); ++p)
*p = 0;
return 0;
}
#include
using namespace std;
int main() {
int a[5], b[5];
for (int i = 0; i < 5; ++i)
cin >> a[i];
for (int i = 0; i < 5; ++i)
cin >> b[i];
for (int i = 0; i < 5; ++i) {
if (a[i] != b[i]) {
cout << "Not equal." << endl;
break;
}
}
cout << "Equal." << endl;
return 0;
}
// vector
#include
#include
using namespace std;
int main() {
int input;
vector<int> a, b;
for (int i = 0; i < 5; ++i) {
cin >> input;
a.push_back(input);
}
for (int i = 0; i < 5; ++i) {
cin >> input;
b.push_back(input);
}
if (a == b)
cout << "Equal." << endl;
else
cout << "Not equal." << endl;
return 0;
}
cp
遍历字符串ca
,并输出其中每一个字符(相当于输出ca
)。结果是hello
。// string
#include
#include
using namespace std;
int main() {
string a, b;
cin >> a >> b;
if (a > b)
cout << "a > b" << endl;
else if (a < b)
cout << "a < b" << endl;
else
cout << "a == b" << endl;
}
// C 风格
#include
#include
using namespace std;
int main() {
char a[100], b[100];
cin >> a >> b;
if (strcmp(a, b) > 0)
cout << "a > b" << endl;
else if (strcmp(a, b) < 0)
cout << "a < b" << endl;
else
cout << "a == b" << endl;
}
#include
#include
using namespace std;
int main() {
char a[] = "Hello", b[] = " world!";
char c[50];
strcpy(c, a);
strcat(c, b);
cout << c << endl;
}
#include
#include
using namespace std;
int main() {
int a[10] = {};
vector<int> ivec(begin(a), end(a));
return 0;
}
#include
#include
using namespace std;
int main() {
int a[10] = {};
vector<int> ivec(begin(a), end(a));
for (int i = 0; i != ivec.size(); ++i)
a[i] = ivec[i];
return 0;
}
#include
#include
using namespace std;
int main(){
int ia[3][4] = {
{1,2,3,4},
{2,3,4,5},
{3,4,5,6},
};
cout << "Range for:\n";
for (int (&p)[4] : ia)
for (int q : p)
cout << q << " ";
cout << "\n\n";
cout << "Normal for with index:\n";
for (int i = 0; i != 3; ++i)
for (int j = 0; j != 4; ++j)
cout << ia[i][j] << " ";
cout << "\n\n";
cout << "Normal for with pointer:\n";
for (int (*p)[4] = ia; p != ia + 3; ++p)
for (int *q = *p; q != *p + 4; ++q)
cout << *q << " ";
return 0;
}
#include
#include
using namespace std;
using arr_int = int[4];
int main(){
int ia[3][4] = {
{1,2,3,4},
{2,3,4,5},
{3,4,5,6},
};
cout << "Range for:\n";
for (arr_int &p : ia)
for (int q : p)
cout << q << " ";
cout << "\n\n";
cout << "Normal for with index:\n";
for (int i = 0; i != 3; ++i)
for (int j = 0; j != 4; ++j)
cout << ia[i][j] << " ";
cout << "\n\n";
cout << "Normal for with pointer:\n";
for (arr_int *p = ia; p != ia + 3; ++p)
for (int *q = *p; q != *p + 4; ++q)
cout << *q << " ";
return 0;
}
#include
#include
using namespace std;
int main(){
int ia[3][4] = {
{1,2,3,4},
{2,3,4,5},
{3,4,5,6},
};
cout << "Range for:\n";
for (auto &i : ia)
for (auto &j : i)
cout << j << " ";
cout << "\n\n";
cout << "Normal for with index:\n";
for (auto i = 0; i != 3; ++i)
for (auto j = 0; j != 4; ++j)
cout << ia[i][j] << " ";
cout << "\n\n";
cout << "Normal for with pointer:\n";
for (auto i = begin(ia); i != end(ia); ++i)
for (auto j = begin(*i); j != end(*i); ++j)
cout << *j << " ";
return 0;
}