- Learning C++ Functional Programming
- Wisnu Anggoro
- 359字
- 2021-07-02 20:51:36
Placing any objects in the container
Container is an object that is used to store other objects and manage the memory that is used by the objects it contains. An array is a new feature added in C++11 to store the collection of specific data types. It is a sequence container since it stores the same data type objects and arranges them linearly. Let's take a look at the following code snippet:
/* array.cpp */
#include <array>
#include <iostream>
auto main() -> int
{
std::cout << "[array.cpp]" << std::endl;
// Initializing an array containing five integer elements
std::array<int, 10> arr = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// Displaying the original elements of the array
std::cout << "Original Data : ";
for(auto a : arr) std::cout << a << " ";
std::cout << std::endl;
// Modifying the content of
// the 1st and 3rd element of the array
arr[1] = 9;
arr[3] = 7;
// Displaying the altered array elements
std::cout << "Manipulated Data: ";
for(auto a : arr) std::cout << a << " ";
std::cout << std::endl;
return 0;
}
As we can see in the preceding code, we instance a new array named arr, set its length as 10, and only approve the int element. As we can guess, the output of the code is a line of numbers 0 through 9, which is shown in the original data, and the other line will show the altered data, as we can see in the following screenshot:

It is good to use an array as the container since we can store the data and manipulate them. We can also sort and find a specific element if we want. However, since the array is a compile-time non-resizable object, we have to decide the size of the array we intend to use at the very beginning as we cannot change the size later. In other words, we cannot insert or remove the element from the existing array. As a solution to this problem, and for the best practice of using the container as well, we can now use a vector to store our collection. Let's take a look at the following code:
/* vector.cpp */
#include <vector>
#include <iostream>
auto main() -> int
{
std::cout << "[vector.cpp]" << std::endl;
// Initializing a vector containing three integer elements
std::vector<int> vect = { 0, 1, 2 };
// Displaying the original elements of the vector
std::cout << "Original Data : ";
for (auto v : vect) std::cout << v << " ";
std::cout << std::endl;
// Adding two new data
vect.push_back(3);
vect.push_back(4);
// Displaying the elements of the new vector
// and reverse the order
std::cout << "New Data Added : ";
for (auto v : vect) std::cout << v << " ";
std::cout << std::endl;
// Modifying the content of
// the 2nd and 4th element of the vector
vect.at(2) = 5;
vect.at(4) = 6;
// Displaying the altered array elements
std::cout << "Manipulate Data: ";
for (auto v : vect) std::cout << v << " ";
std::cout << std::endl;
return 0;
}
Now, we have a vector instance in our preceding code instead of an array instance. As we can see, we give an additional value for the vector instance using the push_back() method. We can add the value anytime we want. The manipulation of each element is also easier since vector has an at() method that returns a reference to the element of the specific index. The following screenshot is what we will see as the output when running the code:

- 極簡算法史:從數學到機器的故事
- LabVIEW Graphical Programming Cookbook
- 算法精粹:經典計算機科學問題的Java實現
- Learning Elixir
- Apache Karaf Cookbook
- Hands-On RESTful Web Services with Go
- Building an RPG with Unity 2018
- Linux命令行與shell腳本編程大全(第4版)
- Mastering C++ Multithreading
- Zabbix Performance Tuning
- Java程序設計基礎(第6版)
- MongoDB Cookbook(Second Edition)
- Docker on Windows
- Mastering JavaScript Promises
- R語言:邁向大數據之路