- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有一个基本上表示元组(双 x,双 y)的类,我已经重载了 << 运算符,所以我可以打印该类。现在我想对 >> 做同样的事情,所以它只支持以下格式:x, (x) 和 (x,y)。
我有以下代码:
std::ostream & operator<< (std::ostream &output, tuple &c){
output << "(" << c.x << "," << c.y << ")" << endl;
return output;
}
std::istream & operator>> (std::istream & input, tuple &c){
// Check for following patterns: x, (x) or (x,y)
}
我可以遍历输入和正则表达式匹配吗?那怎么办呢?另外我怎么能测试它是否真的有效,就像这样 std::cin >> "(10.2,5.5)"
还是我需要从文件中读取以进行测试?
编辑:给出的答案确实解决了这个问题,但我想添加一种方法来测试它,因为它可能会使用除我以外的其他人:
tuple x(6,2);
stringstream ss;
ss << x;
ASSERT_EQUALS(ss.str(), "(6,2)\n");
最佳答案
正则表达式对于像这样的简单输入任务来说是不必要的。以下是我的做法,不检查输入是否有效,只是解析:
std::istream & operator>> (std::istream & input, tuple &c){
// Check for following patterns: x, (x) or (x,y)
char firstCharacter;
input >> firstCharacter; // See what the first character is, since it varies
if (firstCharacter == '(')
{ // The simplest case, since the next few inputs are of the form n,n)
char delimiters;
input >> c.x >> delimiters >> c.y >> delimiters;
// N , N )
// You also here have to check whether the above inputs are valid,
// such as if the user enters a string instead of a number
// or if the delimeters are not commas or parentheses
}
else if (isdigit(firstCharacter) || firstCharacter == '-')
{ // For negative numbers
char delimiters;
input.unget(); // Put the number back in the stream and read a number
input >> c.x >> delimiters >> delimiters >> c.y >> delimiters;
// N , ( N )
// You also here have to check whether the above inputs are valid,
// such as if the user enters a string instead of a number
// or if the delimeters are not commas or parentheses
}
else
{
// Handle some sort of a parsing error where the first character
// is neither a parenthesis or a number
}
return input;
}
关于C++ 指定来自 istream 的输入并测试它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48664037/
我正在尝试弄清楚如何让一个成员变量代表传入的 istream 或类自己创建的成员变量。 我认为,如果我动态分配类创建的 istream,那么使用指向 istream 的指针可能会起作用;然而,问题在于
我正在尝试理解这段代码 istream &read(istream &is, Sales_data &item) { double price = 0; is >> item.b
我编写了一个实现 IStream(COM 接口(interface)而不是 C++ 标准库类)的过滤器。这一切都很好,但我的问题是我不确定何时(如果有的话)我可以确定不会发送进一步的 IStream
我正在测试 C++PL 书中的一段代码并找到了下一段代码(我不想感觉我是在将它从书中复制粘贴到我的 IDE,所以我至少改变了变量名): istream& operator>> (istream& is
我正在阅读 C++ Practical Programming by Example by Andrew Koenig (Author)并在 4.1.3 Reading homework grades
我正在尝试使用 istream& getline 而不是使用 istream& operator 从文件中读取行。 结果cpp #include "Result.h" Result::Result()
我试图通过套接字连接发送图像,但我遇到了以下代码的问题: //stream to char array STATSTG myStreamStats; ULONG bytesSaved; myStrea
这里有两段代码,我起初认为它们应该是等价的: { std::ifstream stream("test.bin", std::ios_base::in | std::ios_base::bin
我在网上查了,但没找到合适的。 怎么可能有这样的主线(原始主线): int main() { Image left; std::ifstream ifs("l
假设我有以下代码: std::ifstream file(name, flags); file.seekg(0, std::ios::beg); // Check for error file.rea
在 Windows 上的 C++ 中,是否有任何简单的方法可以为现有 std::stream 对象创建 (COM) IStream 接口(interface)? 一个示例是使用 IWICStream:
考虑以下简单程序,它将输入拆分为空白并每行打印出一个非空白标记: #include int main(int argc, char* argv[]) { while (std::ci
istream& getline (istream& is, string& str); 此处 str 在换行符后终止。但是,如果我想处理 str cotents 2-3 行的情况,那么还有什么选择呢
这个问题已经有答案了: Java multiple file transfer over socket (3 个回答) 已关闭 5 年前。 我正在编写一个程序在两台计算机之间传输文件,但我没有关闭套接
void transferData(ifstream& input_file, ofstream& output_file) { char ch; while (input_file
我正在读取包含以下行的文本文件: deltaT 0.005; 在字符串中找到正确的行后,我想使用如下代码读出该值: double deltaT;
我正在查看 istream 类,但没有看到可以完全清除缓冲区并将输入设置为为下一个“干净”输入做好准备的方法。 我为我的类定义了提取运算符,在我的主程序中我要求用户输入,如下所示: while (tr
我正在编写一个解析器,之前我在尝试解析标识符(任何对 C++ 变量名有效的东西)和未闭合的字符串文字(任何以 " 开头的东西)时遇到了麻烦,但是在我的输入末尾缺少结束符 ")。我认为这是因为词法分析器
我得到了一个具有以下签名的函数。我无法改变它,我必须接受它。 void parse(std::istream & in); 我应该测试这个函数,所以基本上用预定义的内容调用它并检查值是否被正确解析。因
我的运算符(operator)有问题>> istream& operator>> (istream& is, Matrix& M) { char first; is>>first;
我是一名优秀的程序员,十分优秀!