当前位置 : 主页 > 编程语言 > c++ >

C++ vector迭代器访问二维数组

来源:互联网 收集:自由互联 发布时间:2021-06-23
#includeiostream #include vector int main(){ std::vector int arr( 5 ); // 创建一维数组 for ( int i= 0 ;i 5 ;++ i) std::cout arr[i] " " ; std::cout std::endl; std::cout std::endl; std::vector int arr1( 10 , 90 ); for (std::vector int :
#include<iostream>
#include<vector>

int main(){
    std::vector<int> arr(5);  // 创建一维数组
    for(int i=0;i<5;++i)
    std::cout << arr[i]<<" ";
    std::cout << std::endl;
    std::cout << std::endl;

    std::vector<int> arr1(10,90);
    for(std::vector<int>::const_iterator i=arr1.begin();i!=arr1.end();++i){
        std::cout << *i<<" ";
    }
    std::cout << std::endl;
    std::cout << std::endl;

    std::vector<std::vector<int>> arr2(4,std::vector<int>(5,20));  // 创建二维数组
    for(int i=0;i<4;++i){
        for(int j=0;j<5;++j) std::cout << arr2[i][j]<<" ";
        std::cout <<std::endl;
    }
    std::cout << std::endl;

    std::vector<std::vector<int>>::iterator toarr2;
    std::vector<int>::iterator it;
    for(toarr2 =arr2.begin(); toarr2 != arr2.end(); ++toarr2){
        for(it=(*toarr2).begin(); it != (*toarr2).end();++it){
            std::cout << *it<<" ";
        } 
        std::cout << std::endl;
    }
    std::cout << std::endl;

    return 0;
}

#include<iostream>#include<vector>int main(){std::vector<int> arr(5);  // 创建一维数组for(int i=0;i<5;++i)std::cout << arr[i]<<" ";std::cout << std::endl;std::cout << std::endl;std::vector<int> arr1(10,90);for(std::vector<int>::const_iterator i=arr1.begin();i!=arr1.end();++i){std::cout << *i<<" ";}std::cout << std::endl;std::cout << std::endl;std::vector<std::vector<int>> arr2(4,std::vector<int>(5,20));  // 创建二维数组for(int i=0;i<4;++i){for(int j=0;j<5;++j) std::cout << arr2[i][j]<<" ";std::cout <<std::endl;}std::cout << std::endl;std::vector<std::vector<int>>::iterator toarr2;std::vector<int>::iterator it;for(toarr2 =arr2.begin(); toarr2 != arr2.end(); ++toarr2){for(it=(*toarr2).begin(); it != (*toarr2).end();++it){std::cout << *it<<" ";} std::cout << std::endl;}std::cout << std::endl;return 0;}

网友评论