如何创建一个功能向量有一个擦除(size_t pos)方法吗?

发布于 2024-11-18 19:43:44 字数 1648 浏览 2 评论 0原文

我正在创建一个 C++ wstring 类,用于 mingw 版本 4.3.0,针对 Win32 进行交叉编译。我希望我的字符串像 std::string 一样工作,这意味着我需要一个 erase(int pos) 方法来擦除位置 pos 处的单个元素代码>.

这是我的第一次尝试:

#include <wchar.h>
#include <iostream>
#include <vector>

class wstring : public std::vector<wchar_t>{
public:
    void erase(size_t where){
    erase(begin()+where);
    }
};

int main(int argc,char **argv) {
    wstring c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');
    c1.erase(1);

    for(size_t i = 0;i<c1.size();i++){
    std::cout << "c1[" << i << "] = " << c1[i] << "\n";
    }
    return 0;
}

这看起来应该对我有用,但是当我尝试编译它时,我收到了这个奇怪的编译器错误:

$ i386-mingw32-g++ x1.cpp
x1.cpp: In member function 'void wstring::erase(size_t)':
x1.cpp:8: error: no matching function for call to 'wstring::erase(__gnu_cxx::__normal_iterator<wchar_t*, std::vector<wchar_t, std::allocator<wchar_t> > >)'
x1.cpp:7: note: candidates are: void wstring::erase(size_t)
$ 

真正奇怪的是,如果我取出 erase 方法并只是内联代码,我没有问题:

#include <wchar.h>
#include <iostream>
#include <vector>

class wstring : public std::vector<wchar_t>{
};

int main(int argc,char **argv) {
    wstring c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');
    c1.erase(c1.begin()+1);

    for(size_t i = 0;i<c1.size();i++){
    std::cout << "c1[" << i << "] = " << c1[i] << "\n";
    }
    return 0;
}

我很困惑。

I am creating a C++ wstring class for use with mingw version 4.3.0, cross-compiling for Win32. I want my string to work like std::string which means that I want an erase(int pos) method that erases a single element at position pos.

Here is my first attempt:

#include <wchar.h>
#include <iostream>
#include <vector>

class wstring : public std::vector<wchar_t>{
public:
    void erase(size_t where){
    erase(begin()+where);
    }
};

int main(int argc,char **argv) {
    wstring c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');
    c1.erase(1);

    for(size_t i = 0;i<c1.size();i++){
    std::cout << "c1[" << i << "] = " << c1[i] << "\n";
    }
    return 0;
}

This looks like it should work to me, but I get this wacko compiler error when I try to compile it:

$ i386-mingw32-g++ x1.cpp
x1.cpp: In member function 'void wstring::erase(size_t)':
x1.cpp:8: error: no matching function for call to 'wstring::erase(__gnu_cxx::__normal_iterator<wchar_t*, std::vector<wchar_t, std::allocator<wchar_t> > >)'
x1.cpp:7: note: candidates are: void wstring::erase(size_t)
$ 

What's really weird is that if I take out the erase method and just inline the code, I have no problem:

#include <wchar.h>
#include <iostream>
#include <vector>

class wstring : public std::vector<wchar_t>{
};

int main(int argc,char **argv) {
    wstring c1;
    c1.push_back(L'a');
    c1.push_back(L'b');
    c1.push_back(L'c');
    c1.erase(c1.begin()+1);

    for(size_t i = 0;i<c1.size();i++){
    std::cout << "c1[" << i << "] = " << c1[i] << "\n";
    }
    return 0;
}

I'm mystified.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

删除→记忆 2024-11-25 19:43:44

您的特定问题的答案是使用 std::vector::erase( iterator ) 而不是 remove

std::vector<int> v;
v.push_back( 1 );
v.push_back( 2 );
v.push_back( 3 );
v.erase( v.begin()+1 ); // equivalent to v.remove( 1 )

但我不认为您在吠叫在右边的树上。标准库中已经有一个 std::wstring ,它是 basic_stringwchar_t 的实例化,并且会尽可能接近它可以到 std::string (这是带有 char 的同一模板的实例化)

The answer to your particular question is to use std::vector<T>::erase( iterator ) instead of remove:

std::vector<int> v;
v.push_back( 1 );
v.push_back( 2 );
v.push_back( 3 );
v.erase( v.begin()+1 ); // equivalent to v.remove( 1 )

But I don't think that you are barking at the right tree. There is already a std::wstring in the standard library that is the instantiation of basic_string with wchar_t, and that will get as close as it can to std::string (which is an instantiation of the same template with char)

走走停停 2024-11-25 19:43:44

您始终可以只使用现有类型 std::wstring,它是 std::basic_string 的 typedef。您也可以根据您最喜欢的整数类型随意创建其他字符串类。

请注意,有相应的流对象 std::wcout 等。

You could always just use the existing type std::wstring, which is a typedef for std::basic_string<wchar_t>. Feel free to make other string classes based on your favourite integral types, too.

Note that there are corresponding stream objects std::wcout etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文