如何在C中获取集合中的元素?

发布于 2024-08-16 03:30:45 字数 44 浏览 4 评论 0原文

我对如何获取集合中的元素感到困惑。我想我必须使用迭代器,但如何逐步执行它?

I am confused as to how to get the elements in the set. I think I have to use the iterator but how do I step through it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

菩提树下叶撕阳。 2024-08-23 03:30:45

type 替换为 int.. 将 var 替换为集合的名称

for (set<type>::iterator i = var.begin(); i != var.end(); i++) {
   type element = *i;
}

最好的方法是使用 boost::foreach。上面的代码将简单地变为:

BOOST_FOREACH(type element, var) {
   /* Here you can use var */
}

您还可以执行 #define foreach BOOST_FOREACH 这样您就可以执行以下操作:

foreach(type element, var) {
   /* Here you can use var */
}

例如:

foreach(int i, name_of_set) {
   cout << i;
}

Replace type with, for example, int.. And var with the name of the set

for (set<type>::iterator i = var.begin(); i != var.end(); i++) {
   type element = *i;
}

The best way though is to use boost::foreach. The code above would simply become:

BOOST_FOREACH(type element, var) {
   /* Here you can use var */
}

You can also do #define foreach BOOST_FOREACH so that you can do this:

foreach(type element, var) {
   /* Here you can use var */
}

For example:

foreach(int i, name_of_set) {
   cout << i;
}
琉璃繁缕 2024-08-23 03:30:45

使用迭代器:

std::set<int> si;
/* ... */
for(std::set<int>::iterator it=si.begin(); it!=si.end(); ++it)
    std::cout << *it << std::endl;

请注意,MSDN 和 cplusplus.com 等许多参考资料都提供了示例 - 一个示例。 ;)

Use iterators:

std::set<int> si;
/* ... */
for(std::set<int>::iterator it=si.begin(); it!=si.end(); ++it)
    std::cout << *it << std::endl;

Note that many references like MSDN and cplusplus.com provides examples - one example. ;)

朱染 2024-08-23 03:30:45

对于 C++11 及更高版本:

std::set<int> my_set;
for (auto item : my_set)
    std::cout << item << endl;

For C++11 and newer:

std::set<int> my_set;
for (auto item : my_set)
    std::cout << item << endl;
苏璃陌 2024-08-23 03:30:45

要列出集合中的所有元素,您可以执行以下操作:

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

int main ()
{
  int myints[] = {1,2,3,4,5};
  set<int> myset (myints,myints+5);

  set<int>::iterator it;

  cout << "myset contains:";
  for ( it=myset.begin() ; it != myset.end(); it++ )
    cout << " " << *it;

  cout << endl;

  return 0;
}

要检查集合中是否有特定元素,您可以使用 set STL 类中的 find() 方法

To list all the elements in the set you can do something like:

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

int main ()
{
  int myints[] = {1,2,3,4,5};
  set<int> myset (myints,myints+5);

  set<int>::iterator it;

  cout << "myset contains:";
  for ( it=myset.begin() ; it != myset.end(); it++ )
    cout << " " << *it;

  cout << endl;

  return 0;
}

To check if a specific elements in the set or not you can use the find() method from the set STL class

看春风乍起 2024-08-23 03:30:45

我喜欢在 VS2010 Beta2 中使用 C++0x lambda 语法看到的内容:

std::for_each( s.begin(), s.end(), 
               [](int value)
               { 
                  // what would be in a function operator() goes here.
                  std::cout << value << std::endl; 
               } );

I'm liking what I'm seeing in VS2010 Beta2 using C++0x lambda syntax:

std::for_each( s.begin(), s.end(), 
               [](int value)
               { 
                  // what would be in a function operator() goes here.
                  std::cout << value << std::endl; 
               } );
攀登最高峰 2024-08-23 03:30:45
set<int> os;
for (auto itr  = os.begin(); itr != os.end() ; ++itr) cout << *itr << endl;
set<int> os;
for (auto itr  = os.begin(); itr != os.end() ; ++itr) cout << *itr << endl;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文