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

Posted 软件工程小施同学

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ std::set emplace 返回值 first second相关的知识,希望对你有一定的参考价值。

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;
}

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

以上是关于C++ std::set emplace 返回值 first second的主要内容,如果未能解决你的问题,请参考以下文章

C++ std::set insert 失败 原因和解决方案 operator

c++顺序容器

C++ std::set<,> operator怎么用

leetcode题解之Find the Duplicate Number

linux C++获取两个std::set容器差异(容器元素差异)(容器元素差别)std::set_differencestd::inserter

linux C++获取两个std::set容器差异(容器元素差异)(容器元素差别)std::set_differencestd::inserter