官术网_书友最值得收藏!

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:

There is no performance issue if we declare an array using std::array; we use in the array.cpp code and compare it with a usual array as we use in the begin_end.cpp code. However, in modern C++, we are given a new array declaration that has a friendly value semantic, so that it can be passed to or returned from functions by value. Also, the interface of this new array declaration makes it more convenient to find the size, and use it with Standard Template Library ( STL)-style iterator-based algorithms.

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:

It is better to always use the at() method instead of the [] operator when we want to access the specific element by its index in a vector instance. It's because, when we accidentally access the out of range position, the at() method will throw an out_of_range exception. Otherwise, the [] operator will give undefined behavior.
主站蜘蛛池模板: 时尚| 锡林浩特市| 永宁县| 西安市| 安溪县| 西华县| 唐河县| 油尖旺区| 金湖县| 中方县| 西畴县| 阿拉善左旗| 岢岚县| 济源市| 泰顺县| 汽车| 河南省| 宜兴市| 旬阳县| 陇西县| 华蓥市| 花莲市| 万荣县| 巴彦县| 名山县| 漳浦县| 密云县| 伊通| 疏勒县| 衡阳市| 枝江市| 吉木乃县| 丰城市| 安达市| 吉首市| 依安县| 浏阳市| 宣化县| 兴业县| 驻马店市| 巨野县|