C + + Array container detailed

Source: Internet
Author: User

An array container, which is a container for storing arrays, is an extension of the Type C array, and can be manipulated using iterators;

For example, "Std::array<int, 5>", it should be noted that if directly assigned, "Std::array<int, 5> ia = {1, 2, 3, 4, 5};"

There will be a warning under gcc: "Missing braces around initializer for ' std::array<int, 5u>::value_type [5] {aka int [5]} ' [-wmissing-bra CES] "

The reason is not in the way of initializing the array, add a group of "{}", such as: "Std::array<int, 5> ia ={{1, 2, 3, 4, 5}};", so that the parameter satisfies the int[5], and then assigns the value;

Arrays are usually assigned in the initialization process, if you want to replace the existing values, one method is to traverse all the values, more complex;

Another method is to reproduce the value by copying to achieve a fast assignment;

Code:

* * * test.cpp * *  Created on:2013.11.12 *      author:caroline
      
//*eclipse CDT; 4.7.1*/
      
#include <iostream>  
#include <array>  
      
int main (void) {  
      
    std::array<int, 5 > ia = {1, 2, 3, 4, 5}};  
    for (const auto I:ia)  
        std::cout << i << "";  
    Std::cout << Std::endl;  
      
    Std::array<int, 5> ia2; Empty array  
    //ia2 = {1, 2, 3, 4, 5};//error  
    IA2 = ia;  
    for (const auto I:IA2)  
        std::cout << i << "";  
    Std::cout << Std::endl;  
      
    return 0;  
}

Author: csdn Blog spike_king

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/

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.