在C ++中访问集合中的对元素

时间:2017-04-18 13:39:19

标签: c++

我想访问C ++中集合中包含的firstsecond元素。 我有以下代码:

#include <iostream>
#include <set>

using namespace std;

int main()
{
  set < pair<int, int> > pairTest;
  set < pair<int, int> >::iterator pairIt;

  for (int i=1; i<=10; ++i)
  {
    // inserting into the pair set
    pairTest.insert( {i, i*10} );
  }

  int m = pairTest.size();

  cout << "pairTest elements are: ";
  for (pairIt = pairTest.begin();pairIt!=pairTest.end();pairIt++)
  {
    // I have the problem in the following
    cout << *pairIt.first << " ";
  }

  return 0;
}

然而,显然它不是正确的方法,我一直在寻找stackoverflow和其他网站,但仍然无法找到答案。 我怎么能解决这个问题?

1 个答案:

答案 0 :(得分:3)

这是因为您首先访问select * from ( select extract(year from activitydate) as yr, extract(month from activitydate) as mon, Sum(dicalls) from updstats where activitydate between to_date(:STARTDATE, 'MM/DD/YYYY') and to_date(:ENDDATE, 'MM/DD/YYYY') group by extract(year from activitydate), extract(month from activitydate) ) ty join ( select extract(year from activitydate) as Pyr, extract(month from activitydate) as Pmon, Sum(dicalls) from updstats where activitydate between add_months(to_date(:STARTDATE, 'MM/DD/YYYY'), -12) and add_months(to_date(:ENDDATE, 'MM/DD/YYYY'), -12) group by extract(year from activitydate), extract(month from activitydate) ) ly on ty.yr = ly.Pyr + 1 and ty.mon = ly.Pmon order by ty.yr, ty.mon 成员,然后尝试取消引用它(first优先于.)且*成员不是指针,因此您无法取消引用它。你必须反过来做,并通过添加括号给出明确的优先权:

first

在C ++中有一个漂亮的&#34;快捷操作符&#34; (与上面的代码完全相同):

(*pairIt).first