奇怪的std::cout错误
C++: Weird std::cout error
我认为这将是一个相对简单的事情:用尾斜杠附加"www.google.ie",并用"http://"前置,结果是一个值为"http://www.google.ie/"的字符串。不,这不是家庭作业……(我知道)
下面是我的代码:std::string line=split1[0]; //split1[0] is "Host: www.google.ie"
std::vector<std::string> split2;
boost::split(split2,line,boost::is_any_of(" "));
boost::erase_all(split2[1],"n");
std::cout<<"split2[1]:"<<split2[1]<<std::endl; //outputs www.google.ie ok
fURL="http://"+split2[1]+"/";
//fURL="http://www.google.ie/"; //this is what I want fURL to be!
std::cout<<std::endl; //just for some testing
std::cout<<std::endl;
std::cout<<std::endl;
std::cout<<std::endl;
std::cout<<std::endl;
std::cout<<"fURL:"<<fURL<<std::endl; //should output: http://www.google.ie/?
这是我奇怪的输出:
split2[1]:www.google.ie /URL:http://www.google.ie
我不知道'/URL:'中的'/'是从哪里来的。就好像我指定的尾斜杠以某种方式被加到了前面。我真的不明白这是怎么可能的…
在Linux上使用g++ 4.5.2
如有任何见地,不胜感激。
提前致谢
我猜这一行
//split1[0] is "Host: www.google.ie"
和你说的不一样。例如,如果你通过http获取,你会有
//split1[0] is "Host: www.google.iern"
,删除n后为
//split1[0] is "Host: www.google.ier"
则fURL为
卷起= " http://" + split2[1] +"/";//http://www.google.ie r/
std::cout<<"fURL:"<<fURL<<std::endl
将打印
卷起:http://www.google.ie
转到第一列(r)并打印'/'覆盖第一个字符'f'
这是你的代码,放在一个小程序中,只有一个调用你的代码包装在foo()函数中。它像你期望的那样工作,没有像你观察到的那样奇怪。如果我遇到像你这样的问题,我总是用那些"奇怪"的代码写一个小程序。正如其他人所暗示的那样,一定有别的东西使事情出错。这里,试一下:
#include <iostream>
#include <vector>
#include <string>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/erase.hpp>
using namespace std;
void foo(const char **split1)
{
std::string line = split1[0]; //split1[0] is "Host: www.google.ie"
std::vector<std::string> split2;
boost::split(split2,line,boost::is_any_of(" "));
boost::erase_all(split2[1],"n");
std::cout<<"split2[1]:"<<split2[1]<<std::endl; //outputs www.google.ie ok
string fURL="http://"+split2[1]+"/";
//fURL="http://www.google.ie/"; //this is what I want fURL to be!
std::cout<<std::endl; //just for some testing
std::cout<<std::endl;
std::cout<<std::endl;
std::cout<<std::endl;
std::cout<<std::endl;
std::cout<<"fURL:"<<fURL<<std::endl; //should output: http://www.google.ie/?
}
int main()
{
const char *split = "Host: www.google.ie";
const char *split1[1];
split1[0] = split;
foo(split1);
return 0;
}
相关文章:
- std::cout输出int时出现编译错误
- C++代码停止工作错误使用cout内部函数
- cout 新创建的对象引发运行时错误
- 错误:在尝试检测 std::cout 是否<< t 时,功能强制转换为数组类型;有效
- 错误:三元运算符无法在对象中正常工作"cout"
- 如何摆脱错误c2061;语法错误 cout?
- 我收到一个断言错误,但是当我编写 cout 语句时,它会消失
- 删除cout会导致分段错误
- 如何解决"cout was not declared in this scope"错误?
- std::cout << glGetString(GL_RENDER) << std::endl;抛出错误但不GL_Renderer或GL_Verision,我不知道为什么?
- 一个语句中没有多个 cout 的错误/警告
- C++ do/while 循环和 if/else: 骰子游戏 -- 第 75 行错误:"Cout"之前的预期"While" -- 如何解决这个问题?
- 为什么<<低;和 cout<<v.begin;单独给出错误,但 cout <<(lower.begin());没有给出任何错误,并使代码易于编译
- 如何修复" no match for 'operator<<' in 'std::cout"错误?
- 每当尝试打印函数变量时,C++ cout 错误
- cout错误地打印出字符串的Vector
- C++字符串cout错误
- 使用函数时发生cout错误
- 过载<<比如cout错误
- 奇怪的std::cout错误