2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
🌈个人主页:Classmate Yuchen
💫个人格言:“成为自己未来的主人~”
When the parameter type of the vector construction is char type, it is very similar to string, but there are also differences between the two. For example, for vector,It can take into account other different types, while string can only be used for strings., and after we study string in detail, it will become easier for us to learn other STL things.
- void test_vector1()
- {
- vector<double> v2;
- vector<int> v1;
- v1.push_back(1);
- v1.push_back(2);
- v1.push_back(3);
- v1.push_back(4);
- for (size_t i = 0; i < v1.size(); i++)
- {
- cout << v1[i] << " ";
- }
- cout << endl;
- }
- int main()
- {
- test_vector1();
- return 0;
- }
In fact, there is nothing much to say about push_back. It will add characters to the end of the original storage space of the Vector.
In fact, there is a detailed introduction in the document. When there is not enough space during insertion, the capacity will be automatically expanded.
As for the traversal methods of Vector functions, we will also talk about multiple methods here:
- for (size_t i = 0; i < v1.size(); i++)
- {
- cout << v1[i] << " ";
- }
- cout << endl;
- for (size_t i = 0; i < v1.size(); i++)
- {
- cout << v1[i] << " ";
- }
- cout << endl;
- vector<int>::iterator it1 = v1.begin();
- while (it1 != v1.end())
- {
- cout << *it1 << " ";
- it1++;
- }
- cout << endl;
- for (auto e : v1)
- {
- cout << e << " ";
- }
- cout << endl;
Including subscript [] access, iterators and range for, which are basically the same as the string we talked about earlier.
- void test_vector2()
- {
- vector<string> v2;
- string s1("张三");
- v2.push_back(s1);
- v2.push_back(string("李四"));
- v2.push_back("王五");
-
- v2[1] += "来";
- for (const auto& e : v2)
- {
- cout << e << " ";
- }
- cout << endl;
- }
- int main()
- {
- test_vector2();
- return 0;
- }
What’s even more amazing about Vector is that it can be reused. You can apply other things inside Vector, such as List, string, or even another Vector.
If string is applied, then each element of the sequence list represents a String.