📜  打印矢量元素的不同方法

📅  最后修改于: 2022-05-13 01:54:21.046000             🧑  作者: Mango

打印矢量元素的不同方法

向量与动态数组相同,能够在插入或删除元素时自动调整自身大小,其存储由容器自动处理。向量的元素被放置在连续的存储中,以便可以使用迭代器访问和遍历它们。在向量中,数据插入在末尾。最后插入需要不同的时间,因为有时可能需要扩展阵列。删除最后一个元素只需要恒定的时间,因为不会发生大小调整。在开始或中间插入和擦除在时间上是线性的。

C++ 中打印 Vector 的所有元素的不同方法

通过使用重载 << 运算符通过在全局范围内重载 <<运算符作为模板函数,可以通过一一迭代来打印向量的所有元素。

下面是实现上述概念的 C++ 程序:

C++
// C++ program to print the elements
// of the vector
#include 
#include 
using namespace std;
 
template 
ostream& operator<<(ostream& os,
                    const vector& vector)
{
    // Printing all the elements
    // using <<
    for (auto element : vector) {
        os << element << " ";
    }
    return os;
}
 
// Driver Code
int main()
{
    // vector containing integer elements
    vector A = { 10, 20, 30,
                      40, 50, 60 };
 
    cout << A << endl;
 
    return 0;
}


C++
// C++ program to print the elements
// of the vector using separators
#include 
#include 
using namespace std;
 
template 
 
// with_separator() function accepts
// two  arguments i.e., a vector and
// a separator string
void with_separator(const vector& vec,
                    string sep = " ")
{
    // Iterating over all elements of vector
    for (auto elem : vec) {
        cout << elem << sep;
    }
 
    cout << endl;
}
 
// Driver Code
int main()
{
    // Vector containing integer items
    vector int_vec{ 10, 20, 30,
                         40, 50, 60 };
 
    // Printing all elements in vector
    with_separator(int_vec, ", ");
 
    // Vector containing string
    vector str_vec{ "One", "Two",
                            "Three", "Four",
                            "Five" };
 
    // Printing all elements in vector
    with_separator(str_vec, ", ");
    return 0;
}


C++
// C++ program to print the elements
// of the vector by using the index
#include 
#include 
using namespace std;
 
template 
void using_index(const vector& vector,
                 string sep = " ")
{
    // Iterating vector by using index
    for (int i = 0; i < vector.size(); i++) {
        // Printing the element at
        // index 'i' of vector
        cout << vector[i] << sep;
    }
    cout << endl;
}
 
// Driver Code
int main()
{
    // Vector containing all integers
    // elements
    vector int_vec{ 10, 20, 30,
                         40, 50, 60 };
 
    // calling using_index() method
    using_index(int_vec);
 
    return 0;
}


C++
// C++ program to print the elements
// of the vector by using iterators
#include 
#include 
#include 
using namespace std;
 
// Driver Code
int main()
{
    // Vector containing all integer
    // elements
    vector vec_of_nums{ 10, 20, 30,
                             40, 50, 60 };
 
    // Printing all elements in vector
    // Element type provided int while
    // calling copy()
    copy(vec_of_nums.begin(),
         vec_of_nums.end(),
         ostream_iterator(cout, " "));
 
    return 0;
}


C++
// C++ program to print the elements
// of the vector
#include 
#include 
#include 
using namespace std;
 
// Driver Code
int main()
{
    // Vector containing integers
    vector int_vec = { 10, 20, 30,
                            40, 50, 60 };
 
    // Printing all elements in vector
    // Element type not provided while
    // calling copy()
    copy(int_vec.begin(),
         int_vec.end(),
         experimental::make_ostream_joiner(cout, " "));
 
    cout << endl;
    return 0;
}


C++
// C++ program to print the elements
// of the vectors
#include 
#include 
#include 
 
// Driver Code
int main()
{
    // Vector containing all
    // integer items
    vector int_vec{ 10, 20, 30,
                         40, 50, 60 };
 
    // Printing all elements in
    // vector
    for_each(int_vec.begin(),
             int_vec.end(),
             [](const auto& elem) {
 
                 // printing one by one element
                 // separated with space
                 cout << elem << " ";
             });
 
    return 0;
}


输出
10 20 30 40 50 60 

以逗号分隔的方式打印:通过避免重载 <<运算符并创建单独的函数,可以提供自定义分隔符来打印向量的内容



下面是实现上述方法的 C++ 程序:

C++

// C++ program to print the elements
// of the vector using separators
#include 
#include 
using namespace std;
 
template 
 
// with_separator() function accepts
// two  arguments i.e., a vector and
// a separator string
void with_separator(const vector& vec,
                    string sep = " ")
{
    // Iterating over all elements of vector
    for (auto elem : vec) {
        cout << elem << sep;
    }
 
    cout << endl;
}
 
// Driver Code
int main()
{
    // Vector containing integer items
    vector int_vec{ 10, 20, 30,
                         40, 50, 60 };
 
    // Printing all elements in vector
    with_separator(int_vec, ", ");
 
    // Vector containing string
    vector str_vec{ "One", "Two",
                            "Three", "Four",
                            "Five" };
 
    // Printing all elements in vector
    with_separator(str_vec, ", ");
    return 0;
}
输出
10, 20, 30, 40, 50, 60,
One, Two, Three, Four, Five, 

通过使用索引:通过使用向量中元素的索引,可以打印所有元素。

下面是实现上述概念的 C++ 程序:

C++

// C++ program to print the elements
// of the vector by using the index
#include 
#include 
using namespace std;
 
template 
void using_index(const vector& vector,
                 string sep = " ")
{
    // Iterating vector by using index
    for (int i = 0; i < vector.size(); i++) {
        // Printing the element at
        // index 'i' of vector
        cout << vector[i] << sep;
    }
    cout << endl;
}
 
// Driver Code
int main()
{
    // Vector containing all integers
    // elements
    vector int_vec{ 10, 20, 30,
                         40, 50, 60 };
 
    // calling using_index() method
    using_index(int_vec);
 
    return 0;
}
输出
10 20 30 40 50 60 

通过提供元素类型没有 for 循环的情况下打印所有元素:可以使用 STL 算法 copy() 打印向量的所有元素。通过在调用 copy() 算法时提供元素类型,可以将向量的所有元素复制到输出流。

下面是实现上述方法的 C++ 程序:

C++

// C++ program to print the elements
// of the vector by using iterators
#include 
#include 
#include 
using namespace std;
 
// Driver Code
int main()
{
    // Vector containing all integer
    // elements
    vector vec_of_nums{ 10, 20, 30,
                             40, 50, 60 };
 
    // Printing all elements in vector
    // Element type provided int while
    // calling copy()
    copy(vec_of_nums.begin(),
         vec_of_nums.end(),
         ostream_iterator(cout, " "));
 
    return 0;
}
输出
10 20 30 40 50 60 

使用 (experimental::make_ostream_joiner) 不提供元素类型:在 Method-1 程序中,观察到在调用 copy() 算法时,向量中专门提供了元素的类型。但是使用 C++ 17 Experiment::make_ostream_joiner,可以在调用 copy() 时打印向量的所有元素,而无需指定向量中的元素类型。

下面是实现上述方法的 C++ 程序:

C++

// C++ program to print the elements
// of the vector
#include 
#include 
#include 
using namespace std;
 
// Driver Code
int main()
{
    // Vector containing integers
    vector int_vec = { 10, 20, 30,
                            40, 50, 60 };
 
    // Printing all elements in vector
    // Element type not provided while
    // calling copy()
    copy(int_vec.begin(),
         int_vec.end(),
         experimental::make_ostream_joiner(cout, " "));
 
    cout << endl;
    return 0;
}
输出
10 20 30 40 50 60 

通过使用Lambda函数可以在向量的每个元素上使用 lambda函数,在函数内部,可以在不提供元素类型的情况下打印元素。

下面是实现上述方法的 C++ 程序:

C++

// C++ program to print the elements
// of the vectors
#include 
#include 
#include 
 
// Driver Code
int main()
{
    // Vector containing all
    // integer items
    vector int_vec{ 10, 20, 30,
                         40, 50, 60 };
 
    // Printing all elements in
    // vector
    for_each(int_vec.begin(),
             int_vec.end(),
             [](const auto& elem) {
 
                 // printing one by one element
                 // separated with space
                 cout << elem << " ";
             });
 
    return 0;
}
输出
10 20 30 40 50 60 
想要从精选的视频和练习题中学习,请查看C++ 基础课程,从基础到高级 C++ 和C++ STL 课程,了解基础加 STL。要完成从学习语言到 DS Algo 等的准备工作,请参阅完整的面试准备课程