emplace返回值


一个​​pair​​ 


  • 逻辑组件如果已插入那就是真的, 如果映射已经包含值相同地排序的元素就是假的.  
  • 返回值的迭代器元素对返回插入新元素的地址 (如果 bool 元素为 true) 或已找到其中的元素 (如果 bool 元素是假)。 

If the function successfully inserts the element (because no equivalent element existed already in the ​​set​​), the function returns a ​​pair​​ of an iterator to the newly inserted element and a value of true.


Otherwise, it returns an iterator to the equivalent element within the container and a value of false.


Member type iterator is a ​​bidirectional iterator​​ type that points to an element.

​pair​​ is a class template declared in ​​<utility>​​ (see ​​pair​​).

​set::emplace - C++ Reference​

// set_emplace.cpp
// compile with: /EHsc
#include <set>
#include <string>
#include <iostream>

using namespace std;

template <typename S> void print(const S& s) {
cout << s.size() << " elements: ";

for (const auto& p : s) {
cout << "(" << p << ") ";
}

cout << endl;
}

int main()
{
set<string> s1;

auto ret = s1.emplace("ten");

if (!ret.second){
cout << "Emplace failed, element with value \"ten\" already exists."
<< endl << " The existing element is (" << *ret.first << ")"
<< endl;
cout << "set not modified" << endl;
}
else{
cout << "set modified, now contains ";
print(s1);
}
cout << endl;

ret = s1.emplace("ten");

if (!ret.second){
cout << "Emplace failed, element with value \"ten\" already exists."
<< endl << " The existing element is (" << *ret.first << ")"
<< endl;
}
else{
cout << "set modified, now contains ";
print(s1);
}
cout << endl;
}

C++ std::set emplace 返回值 first second_c++

​set::emplace - 游戏蛮牛 - C++中文翻译用户手册​​