簡體   English   中英

std::views 尚未聲明

[英]std::views has not been declared

我正在嘗試使用 c++20 中的ranges庫,並且我有這個簡單的循環。

for (const int& num : vec | std::views::drop(2)) {
    std::cout << num << ' ';
}

我收到一條錯誤消息,說error: 'std::views' has not been declared 我沒有收到關於包含 header 的任何錯誤。

這是我的 g++

g++.exe (MinGW-W64 x86_64-ucrt-posix-seh, built by Brecht Sanders) 11.2.0
Copyright (C) 2021 Free Software Foundation, Inc.

據我所知,只要你有 c++20,它就應該可以工作。

以下示例無法在我的機器上編譯:

#include <iostream>
#include <ranges>
#include <vector>

int main() {
    std::cout << "Hello world!";
    std::vector <int> vec = {1, 2, 3, 4, 5};
    // print entire vector
    for (const int& num : vec) {
        std::cout << num << ' ';
    }
    std::cout << "\n";
    // print the vector but skip first few elements
    for (const int& num : vec | std::views::drop(2)) {
        std::cout << num << ' ';
    }
    return 0;
}

根據您的編譯器版本,不同的標准是默認的。 對於11.2 ,它是 C++ 17 AFAIK。
Cou 可以只添加一個標志並編譯:

g++ --std=c++20 main.cc

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM