简体   繁体   English

C ++:奇怪的std :: cout错误

[英]C++: Weird std::cout error

I thought this would be a relatively simple thing to do: append "www.google.ie" with a trailing slash, and pre-pend it with "http://", resulting in a string with the value "http://www.google.ie/". 我认为这是相对简单的事情:在“ www.google.ie”后面加上斜杠,并在其前面加上“ http://”,从而得到一个值为“ http://”的字符串www.google.ie/”。 No, this is not homework... (I know) 不,这不是作业……(我知道)

Now here is my code: 现在这是我的代码:

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/?

And here is my weird output: 这是我的奇怪输出:

 split2[1]:www.google.ie /URL:http://www.google.ie 

I have no idea where the '/' in '/URL:' is coming from. 我不知道“ / URL:”中的“ /”来自何处。 It's as if the trailing slash I specified is being tacked on to the front somehow. 好像我指定的尾部斜杠以某种方式固定在前面。 I really don't understand how this is possible... 我真的不明白这怎么可能...

Using g++ 4.5.2 on Linux Ubuntu. 在Linux Ubuntu上使用g ++ 4.5.2。

Any insight greatly appreciated. 任何见解非常感谢。

Many thanks in advance, 提前谢谢了,

I guess that this line 我想这条线

//split1[0] is "Host: www.google.ie" // split1 [0]是“主持人:www.google.ie”

is something different than what you say. 与您所说的有所不同。 If you got it over http for example you would have 例如,如果您通过http获得它,

//split1[0] is "Host: www.google.ie\\r\\n" // split1 [0]是“主机:www.google.ie \\ r \\ n”

which after deleting \\n is 删除\\ n之后是

//split1[0] is "Host: www.google.ie\\r" // split1 [0]是“主机:www.google.ie \\ r”

Then fURL is 然后fURL是

fURL="http://"+split2[1]+"/"; fURL =“ http://” + split2 [1] +“ /”; // http://www.google.ie \\r/ // http://www.google.ie \\ r /

This 这个

std::cout<<"fURL:"<<fURL<<std::endl

will print 将打印

fURL:http://www.google.ie 网址:http://www.google.ie

go to the first column (\\r) and print '/' overwriting first character 'f' 转到第一列(\\ r)并打印'/'覆盖第一个字符'f'

This is your code, put into a little program with just that one call to your code wrapped in a foo()-function. 这是您的代码,放在一个小程序中,只需将对您代码的一个调用包装在foo()函数中。 It works as you would expect, and does nothing weird like you are observing. 它可以按您期望的那样工作,并且没有您所观察到的任何奇怪的事情。 If I run into a problem like you have I always write a little program with just that code that is "weird". 如果遇到像您这样的问题,我总是只用“怪异”的代码编写一个小程序。 As suggested by the others, there has to be something else, that is making things go wrong. 正如其他人所建议的那样,还必须有其他东西使事情出错。 Here, try it out: 在这里,尝试一下:

#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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM