Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 什么是'std::set<;int,int>;::迭代器`?_C++_Iterator_Set_C++14_C++ Standard Library - Fatal编程技术网

C++ 什么是'std::set<;int,int>;::迭代器`?

C++ 什么是'std::set<;int,int>;::迭代器`?,c++,iterator,set,c++14,c++-standard-library,C++,Iterator,Set,C++14,C++ Standard Library,我在网上看到了下面这个奇怪的代码片段(简化) #包括 #包括 使用名称空间std; int main(){ 集myset{1,2,3,4,5}; set::iterator it=myset.begin(); cout第二个模板参数无效,这是正确的。这违反了容器的前提条件,使得整个事件具有未定义的行为。但是这里通常有几件事情需要解包,因此我们可以对此进行一些推理。关键点如下: 实例化必须以某种方式使用无效的模板参数来触发失败。并且必须在与类定义一起实例化的位置使用该参数。并非总是实例化整个类。一

我在网上看到了下面这个奇怪的代码片段(简化)

#包括
#包括
使用名称空间std;
int main(){
集myset{1,2,3,4,5};
set::iterator it=myset.begin();

cout第二个模板参数无效,这是正确的。这违反了容器的前提条件,使得整个事件具有未定义的行为。但是这里通常有几件事情需要解包,因此我们可以对此进行一些推理。关键点如下:

  • 实例化必须以某种方式使用无效的模板参数来触发失败。并且必须在与类定义一起实例化的位置使用该参数。并非总是实例化整个类。一个著名的示例是成员函数体。只有在实际调用时,才根据需要实例化它们

  • 迭代器
    类型可能是一个别名。此外,它可能是多个不同集合专门化共享的类型的别名。在这种情况下,
    ::迭代器
    仅依赖于第一个模板参数,这意味着您创建的类型可能与另一个
    b返回的类型相同egin


  • 但归根结底,这是一个格式不正确的程序。混合不同容器中的迭代器本身是未定义的,但在此之前还有一个先决条件冲突。总之,这不值得强调,因为这样的代码不应该出现在任何地方,除非在智力练习中。

    第二个模板参数是not使用,当您访问迭代器类型时,因此没有错误。
    #include <iostream>
    #include <set>
    
    using namespace std;
    
    int main() {
        set<int> myset{1, 2, 3, 4, 5};
        set<int, int>::iterator it = myset.begin();
        cout << *it << endl;
        return 0;
    }