int main()
{
int arr[10];
for (int ix = 0; ix < 10; ix++)
arr[ix] = ix;
return 0;
}
#include
using std::vector;
int main()
{
int arr1[10], arr2[10];
for (int ix = 0; ix < 10; ix++)
arr1[ix] = ix;
for (int ix = 0; ix < 10; ix++)
arr2[ix] = arr1[ix];
vector<int>ivec1, ivec2;
for (int ix = 0; ix < 10; ix++)
ivec1.push_back(ix);
ivec2 = ivec1;
return 0;
}
不初始化scores,则scores里面所有元素的初始值将是未定义。
p1 += p2 - p1;
功能是将p1指向p2指向的元素。
当p1是常量指针时非法。
int main()
{
int arr[100];
for (auto p = arr; p < arr + 100; p++)
{
*p = 0;
}
return 0;
}
#include
#include
using std::cout;
using std::endl;
using std::vector;
int main()
{
int arr1[10], arr2[10];
bool isSame = true;
for (int i = 0; i < 10; i++)
{
arr1[i] = i;
arr2[i] = 9 - i;
}
for (int i = 0; i < 10; i++)
if (arr1[i] != arr2[i])
isSame = false;
cout << (isSame ? "Yes" : "No") << endl;
vector<int>ivec1, ivec2;
for (int i = 0; i < 10; i++)
{
ivec1.push_back(i);
ivec2.push_back(9 - i);
}
cout << (ivec1 == ivec2 ? "Yes" : "No") << endl;
return 0;
}
const char ca[] = { 'h','e','l','l','o' };
const char *cp = ca;
while (*cp)
{
cout << *cp << endl;
cp++;
}
将字符串打印出来,最后没有空字符,所以不会跳出循环。
两指针(同一数组)相减表示之间相差距离,指针加上一个整数表示指针移动到某一位置,两个指针相加并没有实际意义。
#include
#include
using std::cin;
using std::cout;
using std::endl;
using std::string;
int main()
{
char ch1[10] = "hello";
char ch2[10] = "hello";
bool isSame = true;
for (auto i = 0; i < 5; i++)
{
if (ch1[i] != ch2[i])
isSame = false;
}
cout << (isSame ? "Yes" : "No") << endl;
string str1, str2;
str1 = "hello";
str2 = "hello";
cout << (str1 == str2 ? "Yes" : "No") << endl;
return 0;
}
#include
#include
using std::cin;
using std::cout;
using std::endl;
int main()
{
char ch1[] = "hello ";
char ch2[] = "world!";
char ch3[20];
strcpy_s(ch3, ch1);
strcat_s(ch3, ch2);
cout << ch3 << endl;
return 0;
}
#include
#include
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int main()
{
int arr[10];
for (int ix = 0; ix < 10; ix++)
arr[ix] = ix;
vector<int>ivec(arr, arr + 10);
for (auto ix : ivec)
{
cout << ix << endl;
}
return 0;
}
#include
#include
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int main()
{
vector<int>ivec;
for (int ix = 0; ix < 10; ix++)
ivec.push_back(ix);
int arr[10], ix = 0;
for (auto iter = ivec.begin(); iter != ivec.end(); iter++)
{
arr[ix] = *iter;
ix++;
}
for (auto ix : arr)
{
cout << ix << endl;
}
return 0;
}
#include
using std::cin;
using std::cout;
using std::endl;
int main()
{
int arr[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
//1
for (int(&i)[4] : arr)
for (int j : i)
cout << j << " ";
cout << endl;
//2
for (int i = 0; i < 3; i++)
for (int j = 0; j < 4; j++)
cout << arr[i][j] << " ";
cout << endl;
//3
for (int(*i)[4] = arr; i != arr + 3; i++)
for (int *j = *i; j != *i + 4; j++)
cout << *j << " ";
cout << endl;
return 0;
}
#include
using std::cin;
using std::cout;
using std::endl;
int main()
{
int arr[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
using int_arr = int[4];
//1
for (int_arr &i : arr)
for (int j : i)
cout << j << " ";
cout << endl;
//2
for (int i = 0; i < 3; i++)
for (int j = 0; j < 4; j++)
cout << arr[i][j] << " ";
cout << endl;
//3
for (int_arr *i = arr; i != arr + 3; i++)
for (int *j = *i; j != *i + 4; j++)
cout << *j << " ";
cout << endl;
return 0;
}
#include
using std::cin;
using std::cout;
using std::endl;
int main()
{
int arr[3][4] = {0,1,2,3,4,5,6,7,8,9,10,11};
using int_arr = int[4];
//1
for (auto &i: arr)
for (auto &j : i)
cout << j << " ";
cout << endl;
//2
for (auto i = 0; i < 3; i++)
for (auto j = 0; j < 4; j++)
cout << arr[i][j] << " ";
cout << endl;
//3
for (auto i = arr; i != arr + 3; i++)
for (auto j = i; j != i + 4; j++)
cout << *j << " ";
cout << endl;
return 0;
}