Technology Sharing

C Series-Vector (I)

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

🌈个人主页:Classmate Yuchen 

💫个人格言:“成为自己未来的主人~”   

Introduction and use of Vector

Introduction to Vector

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.

  1. Vector is a sequence container that represents a variable-size array.
  2. Like an array, Vector also uses continuous storage space to store elements, which means that you can use subscripts to access Vector elements, which is as efficient as an array, but unlike an array, its size can be changed dynamically, and its size will be automatically handled by the container.
  3. Essentially, Vector uses a dynamically allocated array to store its elements, and when new elements are inserted, this array needs to be reallocated in order to increase storage space. The approach is to allocate a new array and then move all elements to this array. This is a relatively expensive task in terms of time, because every time a new element is added to the container, Vector does not reallocate the size every time.
  4. Vector allocation strategy: Vector will allocate some extra space to accommodate possible growth because the storage space is larger than the actual storage space required. Different libraries use different strategies to weigh the use and reallocation of space, but in any case, the reallocation should be in logarithmic intervals so that inserting an element at the end is completed in constant time complexity.
  5. Therefore, Vector takes up more storage space in order to gain the ability to manage space and grow dynamically in an efficient manner.
  6. Compared with other dynamic sequence containers, Vector is more efficient when accessing elements. It is relatively efficient to add and delete elements at the end, but it is less efficient for other deletion and insertion operations that are not at the end.

Use of Vector

push_back Function

  1. void test_vector1()
  2. {
  3. vector<double> v2;
  4. vector<int> v1;
  5. v1.push_back(1);
  6. v1.push_back(2);
  7. v1.push_back(3);
  8. v1.push_back(4);
  9. for (size_t i = 0; i < v1.size(); i++)
  10. {
  11. cout << v1[i] << " ";
  12. }
  13. cout << endl;
  14. }
  15. int main()
  16. {
  17. test_vector1();
  18. return 0;
  19. }

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:

  1. for (size_t i = 0; i < v1.size(); i++)
  2. {
  3. cout << v1[i] << " ";
  4. }
  5. cout << endl;
  6. for (size_t i = 0; i < v1.size(); i++)
  7. {
  8. cout << v1[i] << " ";
  9. }
  10. cout << endl;
  11. vector<int>::iterator it1 = v1.begin();
  12. while (it1 != v1.end())
  13. {
  14. cout << *it1 << " ";
  15. it1++;
  16. }
  17. cout << endl;
  18. for (auto e : v1)
  19. {
  20. cout << e << " ";
  21. }
  22. cout << endl;

Including subscript [] access, iterators and range for, which are basically the same as the string we talked about earlier.

  1. void test_vector2()
  2. {
  3. vector<string> v2;
  4. string s1("张三");
  5. v2.push_back(s1);
  6. v2.push_back(string("李四"));
  7. v2.push_back("王五");
  8. v2[1] += "来";
  9. for (const auto& e : v2)
  10. {
  11. cout << e << " ";
  12. }
  13. cout << endl;
  14. }
  15. int main()
  16. {
  17. test_vector2();
  18. return 0;
  19. }

 

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.