我目前正在学习 C++ 并且正在努力理解流。今天我学到了一个很酷的东西,你可以将一个字符串流拆分为多个浮点数/整数,例如:
#include <iostream>
#include <sstream>
using namespace std;
int main() {
stringstream ss("1 2.2");
int val1; float val2;
ss >> val1 >> val2;
cout << "val1: " << val1 << endl
<< "val2: " << val2;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在出现了一个问题,当我使用“1,2.2”代替“1 2.2”作为输入字符串时,这类似于我从 csv 文件中得到的内容。我希望能够编写一些内容,将这样的 csv 字符串转换为与上面示例中相同类型的流。我想它看起来像
ss >> mySpecialPipe >> val1 >> val2;
Run Code Online (Sandbox Code Playgroud)
现在我知道mySpecialPipe应该做几件事:
所以我试图建立这个:
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
using namespace std;
class MySpecialPipe {
private:
char delimiter;
public:
vector<string> …Run Code Online (Sandbox Code Playgroud) 我有一个项目,我广泛使用角度 HttpClient 请求作为流的基础。我很想使用茉莉花弹珠来测试这些请求,因为它似乎是流单元测试的“行业标准”。
然而,我正在努力让整个事情正常工作,要么 HttpTestingController 失败,要么大理石失败。我建立了一个堆栈闪电战来证明我的观点。
不久前,我决定参加文字冒险游戏.我一直想这样做.但它第一次出现了史诗般的失败.这次我越来越近但不在那里.我想我看到了错误:问题是变量没有被带到下一个def.所以我想知道的是如何解决它?
这是描述问题的代码段:
def start():
print "Hello there Knight... Knight? Sir Knight, what's your name?"
name = raw_input("> ")
print "Well sir %s of simpleton. We have a message from the queen, do you want to read it?" % name
rm = raw_input("> ")
rm(rm)
def rm(rm):
if rm == "yes":
print "It says: \n Dear %s, \n Our kingdom is in great danger. Dragon Redpole has captured the beatiful princess. Who ever saves her rom his dangerous castle may marry …Run Code Online (Sandbox Code Playgroud) 我刚开始编程并尝试写一些东西,但(当然)它失败了.在我遇到真正的问题之后:UnboundLocalError.所以为了避开你所有的废墟我把代码剥离到这个:
def test():
try:
i1 = int(i1)
i2 = int(i2)
except ValueError:
print "you failed in typing a number"
def input():
i1 = raw_input('please type a number \n >')
i2 = raw_input('please type a number \n >')
Run Code Online (Sandbox Code Playgroud)
然后我写下来:
>>>input()
please insert a number
> 3
please insert a number
> 2
>>>test()
Run Code Online (Sandbox Code Playgroud)
然后我得到了:
that was not a number
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 7, in test
UnboundLocalError: local variable …Run Code Online (Sandbox Code Playgroud)