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

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.
主站蜘蛛池模板: 张家界市| 嘉兴市| 民县| 二连浩特市| 密云县| 宁化县| 江阴市| 遂昌县| 石河子市| 邮箱| 察隅县| 交城县| 新宾| 潮安县| 怀远县| 兴义市| 溧阳市| 清流县| 即墨市| 安图县| 即墨市| 于田县| 龙岩市| 平果县| 临颍县| 宝鸡市| 上饶县| 洛扎县| 阿城市| 大城县| 林口县| 大安市| 星子县| 静乐县| 赞皇县| 桃园市| 泸西县| 常宁市| 潜江市| 合阳县| 安乡县|