c/c++ 標準庫 迭代器(iterator)

小石王發表於2018-09-10

c/c++ 標準庫 迭代器

begin和end運算子返回的具體型別由物件是否是常量決定,如果物件是常量,begin和end返回const_iterator;如果物件不是常量,返回iteraotor

1,但凡是使用了迭代器的迴圈體,都不要向迭代器所屬的容器新增元素。

2,不能在範圍for迴圈中向vector物件新增元素

標準庫 迭代器(iterator)的小例子

test1~test8

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main(){

  //test1                                                                       
  /*                                                                            
  string s("abc bdc");                                                          
  auto it = s.begin();                                                          
  while(it != s.end()){                                                         
    *it = toupper(*it);                                                         
    ++it;                                                                       
  }                                                                             
  cout << s << endl;                                                            
  */

  //test2                                                                       
  /*                                                                            
  string s("abd ddd");                                                          
  for(auto it = s.begin(); it != s.end() && !isspace(*it); ++it){               
    *it = toupper(*it);                                                         
  }                                                                             
  cout << s << endl;                                                            
  */

  //test3 不可改變vector::const_iterator型別                                    
  /*                                                                            
  vector<int>:: iterator it;                                                    
  vector<int>:: const_iterator it2;                                             
  vector<int> v(5,1);                                                           
  it = v.begin();                                                               
  it2 = v.begin();                                                              
  vector<int> const cv(5,2);                                                    
  //it = cv.begin();//it是可改變的vector::iterator,所以編譯錯誤                
  for(auto s : v){                                                              
    cout << s;                                                                  
  }                                                                             
  cout << endl;                                                                 
  */

  //test4 不可改變string::const_iterator型別                                    
  /*                                                                            
  string::iterator sit;                                                         
  string::const_iterator sit2;                                                  
  string s1("abc");                                                             
  sit2 = s1.begin();                                                            
  while(sit2 != s1.end()){                                                      
    //*sit2 = toupper(*sit2);                                                   
    cout << *sit2;                                                              
    ++sit2;                                                                     
  }                                                                             
  cout << endl;                                                                 
  */

  //test5 cbegin和cend,返回const型別的iterator                                  
  /*                                                                            
  string s1("aaaa");                                                            
  auto it1 = s1.cbegin();                                                       
  //*it1 = `c`;//不可以通過const型別的iterator改變原來的物件                    
  cout << s1 << endl;                                                           
                                                                                
  vector<int> v(4,2);                                                           
  auto vit1 = v.cbegin();                                                       
  //*vit1 = 5;//不可以通過const型別的iterator改變原來的物件                     
  */

  //test6 string迭代器運算                                                      
  /*                                                                            
  string s("abcd");                                                             
  string s1("abc");                                                             
  vector<int> v(5,3);                                                           
  auto it1 = s.begin();                                                         
  auto it2 = s.begin();                                                         
  if(it1 == it2){                                                               
    cout << "=" << endl;                                                        
  }                                                                             
  ++it1;                                                                        
  it1 += 1;                                                                     
  if(it1 > it2){                                                                
    cout << ">" << endl;                                                        
    cout << it1 - it2 << endl;                                                  
  }                                                                             
  string::difference_type juli = it1 - it2;                                     
  cout << juli << endl;                                                         
  */

  //test7 vector迭代器運算                                                      
  /*                                                                            
  vector<string> s(4,"abcd");                                                   
  vector<string> s1(5,"abc");                                                   
  auto it1 = s.begin();                                                         
  auto it2 = s.begin();                                                         
  if(it1 == it2){                                                               
    cout << "=" << endl;                                                        
  }                                                                             
                                                                                
  ++it1;                                                                        
  it1 += 1;                                                                     
  if(it1 > it2){                                                                
    cout << ">" << endl;                                                        
    cout << it1 - it2 << endl;                                                  
  }                                                                             
  vector<string>::difference_type juli = it1 - it2;                             
  cout << juli << endl;                                                         
  */

  //test8 利用迭代器實現二分法                                                  
  vector<string> v{"a","b","c","d","e"};
  string target("b");
  vector<string>::size_type idx = 0;
  auto beg = v.begin();
  auto mid = v.begin() + v.size() / 2;
  auto end = v.end();
  while(mid != end && *mid != target){
    if(target < *mid)
      end = mid;
    else
      beg = mid + 1;
    mid = beg + (end - beg) / 2;
  }
  if(mid == end){
    cout << "not found" << endl;
  }
  else{
    idx = mid - v.begin();
    cout << idx << ":" << *mid << endl;
  }
}

相關文章