如何创建一个功能向量有一个擦除(size_t pos)方法吗?
我正在创建一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的特定问题的答案是使用
std::vector::erase( iterator )
而不是remove
:但我不认为您在吠叫在右边的树上。标准库中已经有一个
std::wstring
,它是basic_string
与wchar_t
的实例化,并且会尽可能接近它可以到std::string
(这是带有char
的同一模板的实例化)The answer to your particular question is to use
std::vector<T>::erase( iterator )
instead ofremove
: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 ofbasic_string
withwchar_t
, and that will get as close as it can tostd::string
(which is an instantiation of the same template withchar
)您始终可以只使用现有类型
std::wstring
,它是std::basic_string
的 typedef。您也可以根据您最喜欢的整数类型随意创建其他字符串类。请注意,有相应的流对象
std::wcout
等。You could always just use the existing type
std::wstring
, which is a typedef forstd::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.