Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

ฟังก์ชัน list::insert() ใน C++ STL


งานนี้เป็นการแสดงฟังก์ชันการทำงานของ list::insert() ใน C++ STL

List ใน STL คืออะไร

List เป็นคอนเทนเนอร์ที่อนุญาตให้แทรกและลบได้ในเวลาคงที่ที่ตำแหน่งใดก็ตามในลำดับ List ถูกนำไปใช้เป็น doubly linked lists List อนุญาตการจัดสรรหน่วยความจำแบบไม่ต่อเนื่อง List ทำงานการแทรก ดึง และย้ายองค์ประกอบได้ดีกว่า array, vector และ deque ใน List การเข้าถึงองค์ประกอบโดยตรงช้า และ list คล้ายกับ forward_list แต่วัตถุ forward_list เป็น singly linked lists และสามารถวนรอบได้เฉพาะไปข้างหน้า

insert() คืออะไร

ฟังก์ชัน list::insert() ใช้สำหรับแทรกองค์ประกอบใน list

  • ใช้แทรกองค์ประกอบที่ตำแหน่งที่ระบุ
  • ใช้แทรก n องค์ประกอบใน list
  • ใช้แทรกช่วงขององค์ประกอบที่ตำแหน่งที่ระบุ

ไวยากรณ์

insert(iterator position, const value_type& val)
insert(iterator position, size_type n, const value_type& value)
insert(iterator position, iterator first, iterator last)

พารามิเตอร์

Val - ระบุองค์ประกอบใหม่ที่จะแทรกใน list

Position - ระบุตำแหน่งในคอนเทนเนอร์ที่องค์ประกอบใหม่จะถูกแทรก

n - จำนวนองค์ประกอบที่จะแทรก

First, last - ระบุ iterator ที่กำหนดช่วงขององค์ประกอบที่จะแทรก

ค่าที่คืน

คืนค่า iterator ที่ชี้ไปยังองค์ประกอบแรกขององค์ประกอบที่แทรกใหม่

ตัวอย่าง

อินพุต List - 50 60 80 90

เอาต์พุต New List - 50 60 70 80 90

อินพุต List - T R E N D

เอาต์พุต New List - T R E N D S

แนวทางการทำงาน

  • ประกาศ List ก่อน
  • พิมพ์ List
  • ประกาศฟังก์ชัน insert()

ด้วยแนวทางด้านบน เราสามารถแทรกองค์ประกอบใหม่ใน list ได้ องค์ประกอบใหม่ควรมีชนิดข้อมูลเดียวกับ list

ตัวอย่าง

// C++ code to demonstrate the working of list insert() function in STL
#include <iostream>
#include <list>
using namespace std;

int main() {
    list<int> lst = { 55, 84, 38, 66, 67 };
    // print the list
    cout << "List: ";
    for(auto x = lst.begin(); x != lst.end(); ++x)
        cout << *x << " ";
    // declaring insert() function
    lst.insert(lst.begin(), 6);
    // printing new list after inserting new element
    cout << "\nNew List: ";
    for(auto x = lst.begin(); x != lst.end(); ++x)
        cout << *x << " ";
    return 0;
}

เอาต์พุต

List: 55 84 38 66 67
New List: 6 55 84 38 66 67

ตัวอย่างที่ 2

// C++ code to demonstrate the working of list insert() function in STL
#include <iostream>
#include <list>
using namespace std;

int main() {
    list<char> lst = { 'F', 'B', 'U', 'A', 'R', 'Y' };
    cout << "List: ";
    for(auto x = lst.begin(); x != lst.end(); ++x)
        cout << *x << " ";
    // declaring insert() function
    lst.insert(next(lst.begin()), 1, 'E');
    // printing new list after inserting new element
    cout << "\nNew List: ";
    for(auto x = lst.begin(); x != lst.end(); ++x)
        cout << *x << " ";
    return 0;
}

เอาต์พุต

List: F B U A R Y
New List: F E B U A R Y

ตัวอย่างที่ 3

// C++ code to demonstrate the working of list insert() function in STL
#include <iostream>
#include <list>
#include <vector>
using namespace std;

int main() {
    list<int> lst = { 10, 44, 34, 98, 15 };
    cout << "List: ";
    for(auto x = lst.begin(); x != lst.end(); ++x)
        cout << *x << " ";
    vector<int> v(2, 17);
    lst.insert(lst.begin(), v.begin(), v.end());
    // printing new list after inserting new element
    cout << "\nNew List: ";
    for(auto x = lst.begin(); x != lst.end(); ++x)
        cout << *x << " ";
    return 0;
}

เอาต์พุต

List: 10 44 34 98 15
New List: 17 17 10 44 34 98 15