英文:
Overloading operator>> to create array from string
问题
我想从一个std::string
创建一个std::array
。
为此,我想重载operator>>
。
我有以下的测试用例:
std::istream& operator>>(std::istream& is, const std::array<double, 3>& a)
{
char p1, p2;
is >> p1;
// 如果失败,提醒用户
for (unsigned int i = 1; i < a.size(); ++i)
{
// 忽略/检查是否为数字的一些操作
}
is >> p2;
// 如果失败,提醒用户
return is;
}
int main()
{
std::string a = "[1 2 3]";
std::array<double, 3> arr;
std::istringstream iss(a);
iss >> arr;
return 0;
}
我希望操作符能够检查字符[
和]
是否处于正确的位置,并使用括号内的元素构造数组。
如何检查提取是否成功?如何检查括号内的字符串是否为数字,如果是,如何从中构造数组?
谢谢!
英文:
I would like to create an std::array
from a std::string
.
For this, I would like to overload the operator>>
.
I have the following test case:
std::istream& operator>>(std::istream& is, const std::array<double, 3>& a)
{
char p1, p2;
is >> p1;
// if fail warn the user
for (unsigned int i = 1; i < a.size(); ++i)
{
// something to ignore/ check if numeric
}
is >> p2;
// if fail warn the user
return is;
}
int main()
{
std::string a = "[1 2 3]";
std::array<double, 3> arr;
std::istringstream iss (a);
iss >> arr;
return 0;
}
I would like for the operator to check if the characters [
and ]
are in the correct place and to construct the array with the elements inside.
How can I do checks if the extraction was successfull? How can I check the string between the parenthesis is numeric and if so construct my array from it?
Kind regards
答案1
得分: 2
我会为你翻译这段代码,以下是翻译的结果:
我会添加一个辅助类,用于检查流中是否存在特定字符,并在存在时将其移除。如果不存在,则设置 failbit。
示例:
#include <cctype>
#include <istream>
template <char Ch, bool SkipWhitespace = false>
struct eater {
friend std::istream& operator>>(std::istream& is, eater) {
if /*constexpr*/ (SkipWhitespace) { // constexpr since C++17
is >> std::ws;
}
if (is.peek() == Ch) // 如果预期的字符存在,则移除它
is.ignore();
else // 否则设置 failbit
is.setstate(std::ios::failbit);
return is;
}
};
然后可以像这样使用它:
std::istream& operator>>(std::istream& is, std::array<double, 3>& a) {
// 使用 `eater` 类模板,将 `[` 和 `]` 作为模板参数:
return is >> eater<'['>{} >> a[0] >> a[1] >> a[2] >> eater<']'>{};
}
int main() {
std::string a = "[1 2 3]";
std::array<double, 3> arr;
std::istringstream iss (a);
// iss.exceptions(std::ios::failbit); // 如果你想在失败时抛出异常
if(iss >> arr) {
std::cout << "success\n";
} else {
std::cout << "fail\n";
}
}
演示 中使用 ,
作为输入的分隔符。
英文:
I'd add a helper class that checks for a certain character in the stream and removes it if it's there. If it's not, sets the failbit.
Example:
#include <cctype>
#include <istream>
template <char Ch, bool SkipWhitespace = false>
struct eater {
friend std::istream& operator>>(std::istream& is, eater) {
if /*constexpr*/ (SkipWhitespace) { // constexpr since C++17
is >> std::ws;
}
if (is.peek() == Ch) // if the expected char is there, remove it
is.ignore();
else // else set the failbit
is.setstate(std::ios::failbit);
return is;
}
};
And it could then be used like this:
std::istream& operator>>(std::istream& is, std::array<double, 3>& a) {
// use the `eater` class template with `[` and `]` as template parameters:
return is >> eater<'['>{} >> a[0] >> a[1] >> a[2] >> eater<']'>{};
}
int main() {
std::string a = "[1 2 3]";
std::array<double, 3> arr;
std::istringstream iss (a);
// iss.exceptions(std::ios::failbit); // if you want exceptions on failure
if(iss >> arr) {
std::cout << "success\n";
} else {
std::cout << "fail\n";
}
}
Demo where ,
is used as separator in the input.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论