Apr 262013
 

I recently compiled my psi4 libraries using C++11 and started using some of the features of this language. There are several new useful features of the language and library additions. Here are some that I have started to use in my code.

  • Tuples – Tuples are containers that can collect different type of data. Here is an example of how I have been using tuples in my code:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    
    // a vector of tuples (double,int,int)
    std::vector<std::tuple<double,int,int>> vec_tuple;
     
    // tuples can be added with std::make_tuple
    vec_tuple.push_back( std::make_tuple(1.0,2,3) );
    vec_tuple.push_back( std::make_tuple(3.0,3,5) );
     
    // Let's sort the vector of tuples
    std::sort(vec_tuple.begin(),vec_tuple.end());
     
    // Loop over the first ten elements of the sorted vector
    for (int i = 0; i < 10; ++i){
         double c;
         int m,n;
         // access the elements by unpacking them with std::tie ...
         std::tie(c,m,n) = vec_tuple[i];
     
        // ... or use the std::get function
        c = std::get<0>(vec_tuple[i]); 
    }
 April 26, 2013  Programming

Sorry, the comment form is closed at this time.