简体   繁体   English

如何在 C++ 中列表的单个位置插入多个不同的值?

[英]How can i insert multiple different values in single position of list in c++?

This code is from geeksforgeeks此代码来自geeksforgeeks

list<int> list1; 

list1.assign(3, 2); 


list<int>::iterator it = list1.begin(); 

 
advance(it, 2); 


list1.insert(it, 5); 

 
cout << "The list after inserting"
     << " 1 element using insert() is : "; 
for (list<int>::iterator i = list1.begin(); 
     i != list1.end(); 
     i++) 
    cout << *i << " "; 

cout << endl; 


list1.insert(it, 2, 7); 

 
cout << "The list after inserting"
     << " multiple elements "
     << "using insert() is : "; 

for (list<int>::iterator i = list1.begin(); 
     i != list1.end(); 
     i++) 
    cout << *i << " "; 

cout << endl; 

It shows the output like this:-它显示如下输出:-

   The list after inserting 1 element using insert() is : 2 2 5 2 
   The list after inserting multiple elements using insert() is: 2 2 5 7 7 2

So, I want to add different values in a single position than the duplication of the same value.所以,我想在一个位置添加不同的值而不是相同值的重复。 Is that even possible?这甚至可能吗? for ex- inserting 1,3,7 in 3rd position of the list.用于在列表的第三个位置插入 1,3,7。

Check the reference .检查参考

You can use one of insert() :您可以使用insert()

template< class InputIt >
void insert( iterator pos, InputIt first, InputIt last);            (until C++11)

template< class InputIt >
iterator insert( const_iterator pos, InputIt first, InputIt last ); (since C++11)

Example:例子:

#include <iostream>
#include <vector>
#include <list>

int main(void) {
    std::list<int> list1 = {2, 2, 5, 2};
    std::vector<int> to_insert = {1, 3, 7};

    std::cout << "before insert:";
    for (int v : list1) std::cout << ' ' << v;
    std::cout << '\n';

    list1.insert(std::next(list1.begin(), 2), to_insert.begin(), to_insert.end());

    std::cout << " after insert:";
    for (int v : list1) std::cout << ' ' << v;
    std::cout << '\n';

    return 0;
}

Output:输出:

before insert: 2 2 5 2
 after insert: 2 2 1 3 7 5 2

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++-如何将多个值插入向量,每个值具有不同的索引? - C++ - How do I insert multiple values into a vector, each with a different index? 如何在链表 C++ 的开头插入一个数字? - How can I insert a number to the beginning of a linked list c++? C++:如何在链表中的某个位置插入节点? - C++: How to insert node at a certain position in a linked list? 如何将 int 值传递给我的成员函数 operator += 以将 int 值插入到 C++ 中的对象列表中? - How can I pass a int value to my member function operator += to insert int values into a object list in c++? 在列表中的第二个位置插入c ++ - Insert in list at second position c++ 如何实际获取值插入此链接列表C ++ - How to actually get the values to insert into this linked-list c++ 如何将值从不同的多个数组传递到新的单个数组? - How can I pass values from different multiple arrays to a new single array? 我如何在 c++ 中实现向量的迭代器插入(const_iterator position,首先是 InputIterator,最后是 InputIterator)? - How can i Implement iterator insert (const_iterator position, InputIterator first, InputIterator last) of vector in c++? 如何在不同的列中插入随机数的值? 查询。 C ++。 SQlite数据库 - How can I insert a random number of values in different columns? querys. C++. SQlite database 如何将函数中的多个值传递给C ++中的不同函数? - How do I pass multiple values in a function to different functions in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM