简体   繁体   English

如何像向量一样通过索引访问集合元素?

[英]how to access set elements by index like vector?

I have made a code that construct a set of sets.我制作了一个构造一组集合的代码。 However, I want to access set index like vector *(vec.begin()+i).Is it possible like accessing vector?但是,我想访问像向量 *(vec.begin()+i) 这样的集合索引。是否有可能像访问向量一样? I got an error,Please help me, Is there any solution我得到一个错误,请帮助我,有什么解决办法


#include <iostream>
#include <set>
using namespace std;

int main ()
{
  int myints[] = {75,23,65,42,13};
  std::set<int> myset (myints,myints+5);

  std::cout << "myset contains:";
  for (int i = 0; i < myset.size(); i++) {
      cout<<*(myset.begin()+i);
  }


  std::cout << '\n';

  return 0;
}

You can with std::next .您可以使用std::next But it's not a good idea for std::set because it must linearly increment the iterator.但这对std::set来说不是一个好主意,因为它必须线性递增迭代器。

auto third = std::next(myset.begin(), 2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM