在 C++ 中推送到列表时的有趣行为
Funny behavior when pushing onto list in c++
我放弃了,我只是想知道为什么 push_back 没有按预期工作。忘记我的代码有多糟糕或太复杂。
我有一个 python 程序,它读取缓冲区,并从列表中逐行显示它。如果最后一行不完整,将其从列表中删除,并在下一次迭代时将其预先附加到缓冲区。
response += s.recv(1024).decode("utf-8")
if response[-1] != '\n':
lines = response.splitlines()
response = lines[-1]
lines = lines[:-1]
else:
lines = response.splitlines()
response = '';
for line in lines:
print("New Line: " + line)
time.sleep(1)
前缀为 "New Line" 所以我可以看到我的 "line"
中没有隐藏多行
这是输出:
New Line: :tmi.twitch.tv 001 k20stitchbot :Welcome, GLHF!
New Line: :tmi.twitch.tv 002 k20stitchbot :Your host is tmi.twitch.tv
New Line: :tmi.twitch.tv 003 k20stitchbot :This server is rather new
New Line: :tmi.twitch.tv 004 k20stitchbot :-
New Line: :tmi.twitch.tv 375 k20stitchbot :-
New Line: :tmi.twitch.tv 372 k20stitchbot :You are in a maze of twisty passages, all alike.
New Line: :tmi.twitch.tv 376 k20stitchbot :>
New Line: :tmi.twitch.tv CAP * ACK :twitch.tv/membership
所以我用 visual studio 作为我的编译器用 C++ 编写了以下代码:
found = start = end = 0;
iResult = recv(ConnectSocket, recvbuf, recvbuflen - 1, 0);
if (iResult > 0)
{
recvbuf[iResult] = '[=12=]';
newString.append(recvbuf);
std::replace(newString.begin(), newString.end(), '\r', ' ');
end = newString.length();
while ((found = newString.find("\n", start)) != -1) {
myList.push_back(newString.substr(start, found));
start = found + 1;
if (start >= end) {
start = end;
}
Sleep(1000);
}
if (newString.back() != '\n')
{
std::string leftOver = newString.substr(start, end);
newString = leftOver;
}
else
{
newString = "";
}
for (myListIt = myList.begin(); myListIt != myList.end(); myListIt++)
{
cout << "New Line: " << *myListIt << "\n";
}
myList.clear();
}
我的输出不太理想,就好像不是从头到尾推到列表上,而是从头推到尾。
New Line: :tmi.twitch.tv 001 k20stitchbot :Welcome, GLHF!
New Line: :tmi.twitch.tv 002 k20stitchbot :Your host is tmi.twitch.tv
:tmi.twitch.tv 003 k20stitchbot :This server is
New Line: :tmi.twitch.tv 003 k20stitchbot :This server is rather new
:tmi.twitch.tv 004 k20stitchbot :-
:tmi.twitch.tv 375 k20stitchbot :-
:tmi.twitch.tv 372 k20stitchbot :You
New Line: :tmi.twitch.tv 004 k20stitchbot :-
:tmi.twitch.tv 375 k20stitchbot :-
:tmi.twitch.tv 372 k20stitchbot :You are in a maze of twisty passages, all alike.
:tmi.twitch.tv 376 k20stitchbot :>
New Line: :tmi.twitch.tv 375 k20stitchbot :-
:tmi.twitch.tv 372 k20stitchbot :You are in a maze of twisty passages, all alike.
:tmi.twitch.tv 376 k20stitchbot :>
New Line: :tmi.twitch.tv 372 k20stitchbot :You are in a maze of twisty passages, all alike.
:tmi.twitch.tv 376 k20stitchbot :>
New Line: :tmi.twitch.tv 376 k20stitchbot :>
子字符串参数为:begin-position、长度
而不是:begin-position、end-position
我放弃了,我只是想知道为什么 push_back 没有按预期工作。忘记我的代码有多糟糕或太复杂。
我有一个 python 程序,它读取缓冲区,并从列表中逐行显示它。如果最后一行不完整,将其从列表中删除,并在下一次迭代时将其预先附加到缓冲区。
response += s.recv(1024).decode("utf-8")
if response[-1] != '\n':
lines = response.splitlines()
response = lines[-1]
lines = lines[:-1]
else:
lines = response.splitlines()
response = '';
for line in lines:
print("New Line: " + line)
time.sleep(1)
前缀为 "New Line" 所以我可以看到我的 "line"
中没有隐藏多行这是输出:
New Line: :tmi.twitch.tv 001 k20stitchbot :Welcome, GLHF!
New Line: :tmi.twitch.tv 002 k20stitchbot :Your host is tmi.twitch.tv
New Line: :tmi.twitch.tv 003 k20stitchbot :This server is rather new
New Line: :tmi.twitch.tv 004 k20stitchbot :-
New Line: :tmi.twitch.tv 375 k20stitchbot :-
New Line: :tmi.twitch.tv 372 k20stitchbot :You are in a maze of twisty passages, all alike.
New Line: :tmi.twitch.tv 376 k20stitchbot :>
New Line: :tmi.twitch.tv CAP * ACK :twitch.tv/membership
所以我用 visual studio 作为我的编译器用 C++ 编写了以下代码:
found = start = end = 0;
iResult = recv(ConnectSocket, recvbuf, recvbuflen - 1, 0);
if (iResult > 0)
{
recvbuf[iResult] = '[=12=]';
newString.append(recvbuf);
std::replace(newString.begin(), newString.end(), '\r', ' ');
end = newString.length();
while ((found = newString.find("\n", start)) != -1) {
myList.push_back(newString.substr(start, found));
start = found + 1;
if (start >= end) {
start = end;
}
Sleep(1000);
}
if (newString.back() != '\n')
{
std::string leftOver = newString.substr(start, end);
newString = leftOver;
}
else
{
newString = "";
}
for (myListIt = myList.begin(); myListIt != myList.end(); myListIt++)
{
cout << "New Line: " << *myListIt << "\n";
}
myList.clear();
}
我的输出不太理想,就好像不是从头到尾推到列表上,而是从头推到尾。
New Line: :tmi.twitch.tv 001 k20stitchbot :Welcome, GLHF!
New Line: :tmi.twitch.tv 002 k20stitchbot :Your host is tmi.twitch.tv
:tmi.twitch.tv 003 k20stitchbot :This server is
New Line: :tmi.twitch.tv 003 k20stitchbot :This server is rather new
:tmi.twitch.tv 004 k20stitchbot :-
:tmi.twitch.tv 375 k20stitchbot :-
:tmi.twitch.tv 372 k20stitchbot :You
New Line: :tmi.twitch.tv 004 k20stitchbot :-
:tmi.twitch.tv 375 k20stitchbot :-
:tmi.twitch.tv 372 k20stitchbot :You are in a maze of twisty passages, all alike.
:tmi.twitch.tv 376 k20stitchbot :>
New Line: :tmi.twitch.tv 375 k20stitchbot :-
:tmi.twitch.tv 372 k20stitchbot :You are in a maze of twisty passages, all alike.
:tmi.twitch.tv 376 k20stitchbot :>
New Line: :tmi.twitch.tv 372 k20stitchbot :You are in a maze of twisty passages, all alike.
:tmi.twitch.tv 376 k20stitchbot :>
New Line: :tmi.twitch.tv 376 k20stitchbot :>
子字符串参数为:begin-position、长度
而不是:begin-position、end-position