New Features of C ++ 11: tuple

Source: Internet
Author: User

We have used pair. pair in C ++ as a template type, which contains two data values. The two data types can be different. pair can be constructed using make_pair.

pair
 
   p = make_pair(1, "a1");
 

If multiple parameters are passed in, you need to nest pair, as shown in the following code:


# Include
 
  
# Include
  Using namespace std; int main (){//
   
    
Note: Use '>' instead of '> 'map In the nested template real parameter list.
    
     
> M; map
     
      
Temp1; temp1.insert (make_pair ("b1", "c1"); map
      
        > Temp2; temp2.insert (make_pair ("a1", temp1); m. insert (make_pair (1, temp2); map
       
         Temp3; temp3.insert (make_pair ("b2", "c2"); map
        
          > Temp4; temp4.insert (make_pair ("a2", temp3); m. insert (make_pair (2, temp4); // traverse the map
         
           >>: Const_iterator itr1; map
          
            >:: Const_iterator itr2; map
           
             : Const_iterator itr3; for (itr1 = m. begin (); itr1! = M. end (); itr1 ++) {cout <itr1-> first <""; itr2 = (itr1-> second ). begin (); cout <itr2-> first <""; itr3 = (itr2-> second ). begin (); cout <itr3-> first <""; cout <itr3-> second <endl;} pair
            
              P = make_pair (1, "a1"); return 0 ;}
            
           
          
         
        
       
      
     
    
   
 

The above method is obviously very troublesome. The variable length parameter template was introduced in C ++ 11, so a new data type was invented: tuple, tuple is an N-tuples, and one can be input, two or more different types of data avoid the ugly practice of nesting pair. Create a tuples through make_tuple () and access the elements of the tuples through get <> ().

#include 
 
  #include 
  
   #include 
   
    using namespace std;int main(){auto t1 = make_tuple(1, "a1", "b1", "c1");cout << get<0>(t1) << " ";cout << get<1>(t1) << " ";cout << get<2>(t1) << " ";cout << get<3>(t1) << " ";cout << endl;vector
    
      > tv;tv.push_back(make_tuple(1, "a1", "b1", "c1"));tv.push_back(make_tuple(2, "a2", "b2", "c2"));vector
     
       >::iterator itr;for (itr=tv.begin(); itr!=tv.end(); itr++){cout << get<0>(*itr) << " ";cout << get<1>(*itr) << " ";cout << get<2>(*itr) << " ";cout << get<3>(*itr) << endl;}return 0;}
     
    
   
  
 



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.