我如何计算向量

问题描述 投票:0回答:1

在 python 上我这样做

x = [1,2,3,4]
print(x)

如何将其转换为c++

我尝试这种方式,但它给了我错误


std::vector<int> x = {1,2,3,4};

cout << x;

错误内容



invalid operands to binary expression ('std::__ndk1::ostream' (aka 'basic_ostream<char>') and 'std::vector<int>')

        std::cout << x;

我也不会那样做

std::vector<int> x = {1,2,3,4};
for (int i: x){
    std::cout << i;
}
c++
1个回答
0
投票

使用以下

for (auto &e : x)
{
   std::cout << e << std::endl;
}

应该可以解决问题

它只是浏览列表

© www.soinside.com 2019 - 2024. All rights reserved.