SetInsert函數是C++ STL庫中set容器類中的成員函數之一,用於向set容器中插入元素。其函數原型如下所示:
std::pair<iterator, bool> insert(const value_type& val)
該函數返回一個std::pair對象,第一個元素是一個迭代器,它指向被插入元素的位置,第二個元素是一個bool類型,它指示元素是否被成功插入。
一、函數的使用方法
在使用set容器的insert函數時,需要傳遞一個value_type類型的參數。value_type是set容器中存儲元素的類型,和set類型定義中所指定的類型相同。對於一個int類型的set,value_type則為int。
如果元素已經存在於set中,則insert操作無法插入該元素,並返回當前元素的迭代器以及bool類型的false值;否則,insert操作會將該元素插入到set中,並返回插入元素的迭代器以及bool類型的true值。下面是一個插入元素的示例:
//創建set對象 std::set<int> s; //插入元素 s.insert(10); s.insert(30); s.insert(20);
二、函數的特性
1. 自動去重
set容器自動對插入的元素進行去重,即set容器中不會存在相同的元素。如果你插入一個已經存在於set中的元素,那麼這個元素不會被插入到set中。下面的示例展示了使用insert函數進行去重的操作:
std::set<int> myset; std::set<int>:iterator it; myset.insert(10); myset.insert(20); myset.insert(30); it = myset.insert(20); if (it.second == false) \ cout << "element already exists" << endl;
2. 元素插入位置
set容器中的元素是按照一定的順序進行存儲的。元素插入時不是按照插入的順序來存儲,而是按照set容器類型定義中指定的元素類型進行排序存儲的,這樣會降低插入元素的複雜度。所以,如果元素插入順序是隨機的,則插入元素的順序和最終的set的順序不一定相同。下面是一個插入元素的示例:
std::set<int> myset; std::set<int>:iterator it; std::pair<std::set<int>:iterator, bool> ret; //插入元素 ret = myset.insert(30); it = ret.first; printf("*it is : %d\n", *it); ret = myset.insert(20); it = ret.first; printf("*it is : %d\n", *it); ret = myset.insert(10); it = ret.first; printf("*it is : %d\n", *it);
三、函數的示例
1. 插入一個元素
下面是一個實例,展示如何使用insert插入一個元素:
std::set<int> myset; std::set<int>::iterator it; //插入元素 it = myset.insert(10); //輸出myset for (it=myset.begin(); it!=myset.end(); ++it) std::cout << ' ' << *it; std::cout << std::endl;
2. 插入多個元素
下面是一個實例,展示如何使用insert插入多個元素:
std::set<int> myset; std::set<int>::iterator it; int arr[] = {10, 20, 30, 40, 50}; myset.insert(arr, arr+5); //輸出myset for (it=myset.begin(); it!=myset.end(); ++it) std::cout << ' ' << *it; std::cout << std::endl;
3. 插入容器中不存在的元素
下面是一個實例,展示如何使用insert插入容器中不存在的元素:
std::set<int> myset; std::set<int>::iterator it; std::pair<std::set<int>::iterator,bool> ret; //插入元素 ret = myset.insert(10); if (ret.second==false) { it=ret.first; std::cout << "element already exists" << std::endl; } ret = myset.insert(20); if (ret.second==false) { it=ret.first; std::cout << "element already exists" << std::endl; } ret = myset.insert(30); if (ret.second==false) { it=ret.first; std::cout << "element already exists" << std::endl; } //輸出myset for (it=myset.begin(); it!=myset.end(); ++it) std::cout << ' ' << *it; std::cout << std::endl;
4. 插入容器中已經存在的元素
下面是一個實例,展示如何使用insert插入容器中已經存在的元素:
std::set<int> myset; std::set<int>::iterator it; std::pair<std::set<int>::iterator,bool> ret; //插入元素 ret = myset.insert(10); if (ret.second==false) { it=ret.first; std::cout << "element already exists" << std::endl; } //再次插入相同的元素 ret = myset.insert(10); if (ret.second==false) { it=ret.first; std::cout << "element already exists" << std::endl; } //輸出myset for (it=myset.begin(); it!=myset.end(); ++it) std::cout << ' ' << *it; std::cout << std::endl;
5. 插入結構體類型元素
set容器可以存儲自定義的結構體類型。下面是一個示例,展示如何使用insert函數插入結構體類型元素:
#include <set> #include <iostream> #include <iomanip> #include <string> typedef struct Student { std::string name; int age; bool operator()(const Student &stu1, const Student &stu2) const { return stu1.name < stu2.name; } }Student; //定義operator()用於自定義排序 int main() { std::set<Student, Student> student_list; std::set<Student, Student>::iterator it; Student s1, s2, s3; s1.name = "Tom"; s1.age = 20; s2.name = "Jerry"; s2.age = 22; s3.name = "Lisa"; s3.age = 19; student_list.insert(s1); student_list.insert(s2); student_list.insert(s3); for (it=student_list.begin(); it!=student_list.end(); ++it) std::cout << std::setw(10) << it->name << std::setw(10) << it->age << std::endl; return 0; }
以上就是C++ STL庫set容器中insert函數的用法、特性及示例。需要注意的是,set容器是按照元素類型進行默認的排序,如果需要按照特定的排序規則來存儲元素,則可以通過自定義函數對象的方式來實現排序。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/297221.html