• C++ 打印 vector


    打印 1D vector

    方法一: 使用基本 for 循环

    #include <iostream>
    #include <vector>
    
    int main() {
        // initial a vector
        std::vector<int> arrays{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    
        for(int i = 0; i < arrays.size(); ++i) {
            std::cout << arrays[i] << " ";
        }
        std::cout << std::endl;
    
        return 0;
    }
    

    方法二: 使用 for each 循环方法

    #include <iostream>
    #include <vector>
    
    int main() {
        // initial a vector
        std::vector<int> arrays{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    
        for(auto array: arrays) {
            std::cout << array << " ";
        }
        std::cout << std::endl;
    
        return 0;
    }
    

    方法三: 使用 iterator 方法

    #include <iostream>
    #include <vector>
    
    int main() {
        // initial a vector
        std::vector<int> arrays{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    
        for(auto it = arrays.begin(); it != arrays.end(); ++it) {
            std::cout << *it << " ";
        }
        std::cout << std::endl;
    
        return 0;
    }
    

    方法四:使用 ostream_iterator

    #include <iostream>
    #include <vector>
    #include <iterator>
    
    int main() {
        // initial a vector
        std::vector<int> arrays{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    
        std::ostream_iterator<int> it(std::cout, " ");
        std::copy(arrays.begin(), arrays.end(), it);
    
        return 0;
    }
    

    方法五:std::for_each

    #include <iostream>
    #include <vector>
    #include <algorithm>
    
    int main() {
        // initial a vector
        std::vector<int> arrays{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    
        std::for_each(arrays.begin(), arrays.end(), [](const auto &i){std::cout << i << " "; });
        std::cout << "
    ";
    
        return 0;
    }
    

    打印 2D vector

    方法一:index for 循环

    #include <iostream>
    #include <vector>
    
    int main() {
        // initial a vector
        std::vector<std::vector<int>> arrays{ {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}};
    
        for(int i = 0; i < arrays.size(); ++i) {
            for(int j = 0; j < arrays[0].size(); ++j) {
                std::cout << arrays[i][j] << " ";
            }
            std::cout << "
    ";
        }
    
        return 0;
    }
    

    方法二: for each 循环

    #include <iostream>
    #include <vector>
    
    int main() {
        // initial a vector
        std::vector<std::vector<int>> arrays{ {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}};
    
        for(auto &array: arrays) {
            for(auto &arr: array ) {
                std::cout << arr << " ";
            }
            std::cout << "
    ";
        }
    
        return 0;
    }
    

    方法三:使用 iterator 指针

    #include <iostream>
    #include <vector>
    
    int main() {
        // initial a vector
        std::vector<std::vector<int>> arrays{ {1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}};
    
        for(auto ita = arrays.begin(); ita != arrays.end(); ++ita) {
            for(auto it = ita->begin(); it != ita->end(); ++it) {
                std::cout << *it << " ";
            }
            std::cout << "
    ";
        }
    
        return 0;
    }
    
  • 相关阅读:
    Python-append()/extend()
    HTML标签
    字符实体
    css选择器
    边框属性
    背景属性
    文本属性
    字体属性
    给标签起名字
    JS数据类型
  • 原文地址:https://www.cnblogs.com/busyboxs/p/12245395.html
Copyright © 2020-2023  润新知