jeudi 1 février 2018

C++ set wants an initializer_list when I try to add a single number

I have a problem that would seem not a lot of people run into. I'm not sure if it's blatantly obvious what to do and I'm not getting it, or I am writing something wrong. I'm using Windows 10, Android Studio. my code segment looks like this.

vector<pair<::google::protobuf::int64,::google::protobuf::int64>> vpLatSortedNodes;
vector<pair<::google::protobuf::int64,::google::protobuf::int64>> vpLonSortedNodes;
set<::google::protobuf::int64> sLats;
set<::google::protobuf::int64> sLons;

set<int> iSet;
for(long i=lIndexLatMin;i<lIndexLatMax;i++){

    sLats.insert(vpLatSortedNodes.at(i).second);//Problem here
}//end for
for(long i=lIndexLonMin;i<lIndexLonMax;i++){
    sLons.insert(vpLonSortedNodes.at(i).second);//Problem here
}//end for

From what I've been told that line should work and it does work in and IOS version.

I get the error:

Parameter type mismatch: Types 'initializer_list<long long int> and 'long long int'

I've looked up the set::insert acceptable calls and I found this

  single element (1)    
    pair<iterator,bool> insert (const value_type& val);
    pair<iterator,bool> insert (value_type&& val);
  with hint (2) 
    iterator insert (const_iterator position, const value_type& val);
    iterator insert (const_iterator position, value_type&& val);
  range (3) 
    template <class InputIterator>
     void insert (InputIterator first, InputIterator last);
  initializer list (4)  
    void insert (initializer_list<value_type> il); 

So my compiler seems to think I'm trying to use the last of the uses. So I understand that it wants me to use an initializer_list for the type of call I am trying to make, but what really confuses me is the example that is also on information page that seems to say I don't need the initializer_list.

Example is here:

  // set::insert (C++98)
  #include <iostream>
  #include <set>

  int main ()
{
    std::set<int> myset;
    std::set<int>::iterator it;
    std::pair<std::set<int>::iterator,bool> ret;

    // set some initial values:
   for (int i=1; i<=5; ++i) myset.insert(i*10);   //HOW DOES THIS WORK?!?!

   ret = myset.insert(20);               // no new element inserted

   if (ret.second==false) it=ret.first;  // "it" now points to element 20

   myset.insert (it,25);                 // max efficiency inserting
   myset.insert (it,24);                 // max efficiency inserting
   myset.insert (it,26);                 // no max efficiency inserting

   int myints[]= {5,10,15};              // 10 already in set, not inserted
   myset.insert (myints,myints+3);

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

   return 0;
} 

This line in particular I tried to see if it worked and I got the same error for (int i=1; i<=5; ++i) myset.insert(i*10);. I searched StackOverflow and no one else has asked this question, which makes me very hesitant to ask myself. I've spent so much time on trying to figure this out that at this point I just want some kind of hint or answer.

Aucun commentaire:

Enregistrer un commentaire