Array
C ++ 11's traversal cycle optimization
format:
for (variable type variable name: array name)
- Here variable types can also be used to fill a member type auto type
- If the conversion is inconsistent and may be, for default forwarding strong
example:
int Students[5]{}; //学号
int num = sizeof(Students) / sizeof(int);
for (int i = 0; i < num; ++i)
{
cout << "请输入第" << i + 1 << "个学生的学号:";
cin >> Students[i];
}
//for (int i : Students) //可以用这个
for (auto i : Students) //也可以用这个
{
cout << i << endl;
}
Review multidimensional arrays
Two-dimensional array traversal:
int students[3][3]
{
{1,2,3},
{4,5,6},
{7,8,9}
};
for (int i = 0; i < 3; i++)
{
for (auto j : students[i]) //用上c++11的新方法,挺好的
{
cout << j << " ";
}
cout << endl;
}
Three-dimensional array is similar, the code is given here:
int students[2][2][2]
{
{
{1,2},
{3,4},
},
{
{5,6},
{7,8},
}
};
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 2; j++)
{
for (auto k : students[i][j])
{
cout << k << " ";
}
cout << endl;
}
cout << endl;
}
Results:
Array Caution:
-
Unless forced to use an array, otherwise not all; because this is a cross-border loopholes commonplace problem, the program is likely to produce unpredictable errors or being attacked!
-
Multi-dimensional arrays in accordance with the operating system memory of most low-dimensional prioritization
array of review
Common Operations
- size () is used to query the size of the container
- fill (const int) for initializing all
- at (num) for secure access to (cross-border will throw an exception)
- Overloaded [], similar to the array access, it will be out of bounds
- It can be used to compare two reload ==
These are the most basic usage!
Sample code:
std::array<int, 3> StusID{};
std::array<int, 3> _StusID{25,25,25};
cout << "size = " << StusID.size() << endl;
StusID.fill(25);
for (int i = 0; i < StusID.size(); i++)
{
//cout << "stu[" << i << "] = " << StusID[i] << endl; //不安全
cout << "stu[" << i << "] = " << StusID.at(i) << endl;
}
if (StusID == _StusID)cout << "相等" << endl;
else cout << "不相等" << endl;
result:
vector of the review
Common Operations
1, the initialization operation
There are five ways, seen below:
std::vector<int> StuID1; //空容器
std::vector<int> StuID2{ 3,4,5 }; //有三个数
std::vector<int> StuID3(5); //有五个数,并全为0
std::vector<int> StuID4(3,50); //3个数,全为50
The results show visit:
2, a common method
- push_back (value) is inserted into the container a number
- assign (num, value) the container is re-initialized values of num value
- clear () empty vessel
- empty () return a container is empty, otherwise 0
- at (num) secure access to the container contents (cross-border Throws)
Sample code:
std::vector<int> StuID1; //定义空容器
StuID1.push_back(5); //插入一个5
StuID1.push_back(10); //插入一个10
for (int i = 0; i < StuID1.size(); i++) //遍历
{
cout << "Stu1 [" << i << "] = " << StuID1.at(i) << " ";
}
cout << endl;
StuID1.assign(3, 8); //重新初始化
for (int i = 0; i < StuID1.size(); i++)
{
cout << "Stu1 [" << i << "] = " << StuID1.at(i) << " ";
}
cout << endl;
StuID1.clear(); //清空
if (StuID1.empty())cout << "空的" << endl;
else cout << "不是空的" << endl;
Results:
thanks to their own hard work! ! !