为什么C++20. G++ MSYS2 Windows中没有std::erase函数

7gs2gvoe  于 2023-02-26  发布在  Windows
关注(0)|答案(2)|浏览(296)

Apparently std::erase was added in C++20, but my compiler isn't letting me use it for some reason.
code

#include <iostream>
#include <vector>
#include <string>
#include <string.h>
using namespace std;

int main() {
    int num1, num2 = 0;
    cin >> num1;
    cin >> num2;
    int sum = num1 + num2;

    vector<char> vec_num1(to_string(num1).begin(), to_string(num1).end());
    vector<char> vec_num2(to_string(num2).begin(), to_string(num2).end());

    std::erase(vec_num1, "0");
    std::erase(vec_num2, "0");

    int removezero1, removezero2, removezerosum = 0;

    for (int v : vec_num1) {
        removezero1 = removezero1 * 10 + v;
    }
    for (int v : vec_num1) {
        removezero2 = removezero2 * 10 + v;
    }
    for (int v : vec_num1) {
        removezerosum = removezerosum * 10 + v;
    }

    if (removezero1 + removezero2 == removezerosum)
    {
        cout << "YES";
    }
    else {
        cout << "NO";
    }

}

error
main.cpp:16:10: error: 'erase' is not a member of 'std' 16 | std::erase(vec_num1, "0"); | ^~~~~ main.cpp:17:10: error: 'erase' is not a member of 'std' 17 | std::erase(vec_num2, "0"); | ^~~~~
gcc version 12.2.0

wfsdck30

wfsdck301#

有关语言标准选项的GCC 12.2文档:
如果没有给出 C++ 方言选项,则缺省值为-std=gnu17。
因此,为了使用C
20特性,您需要指定-std=c++20-std=gnu++20

uqdfh47h

uqdfh47h2#

As it follows from the error message
main.cpp:16:10: error: 'erase' is not a member of 'std' 16 | std::erase(vec_num1, "0"); | ^~~~~ main.cpp:17:10: error: 'erase' is not a member of 'std' 17 | std::erase(vec_num2, "0"); | ^~~~~
you are compiling the program by the compiler that uses C17 features.
You need to say the compiler to use C
20 features as for example -std=c++20.
But in any case your code is wrong because at least in these declarations

vector<char> vec_num1(to_string(num1).begin(), to_string(num1).end());
vector<char> vec_num2(to_string(num2).begin(), to_string(num2).end());
                      ^^^^^^^^^^^^^^^^^^^^^^^  ^^^^^^^^^^^^^^^^^^^^^

there are used iterators of different sequences of temporary created objects. That is there are used invalid ranges.
You need to create intermediate string objects as for example

auto s1 = std::to_string( num1 );
vector<char> vec_num1(s1.begin(), s1.end());
auto s2 = std::to_string( num2 );
vector<char> vec_num2(s2.begin(), s2.end());

And the calls of the function std::erase are also wrong. For example instead of

std::erase(vec_num1, "0");
std::erase(vec_num2, "0");

you need to write

std::erase(vec_num1, '0');
std::erase(vec_num2, '0');

That is instead of string literals you have to use character literals because the value type of the vectors is char .
Also in this declaration

int removezero1, removezero2, removezerosum = 0;

there is initialized by zero only the last declarator removezerosum . You need to write

int removezero1 = 0, removezero2 = 0, removezerosum = 0;

Also the header

#include <string.h>

is redundant. Remove it.

相关问题