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

C++ C++;:奇怪的std::cout错误,c++,string,boost,C++,String,Boost,我认为这将是一件相对简单的事情:在“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,bo

我认为这将是一件相对简单的事情:在“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/?
我不知道“/URL:”中的“/”来自哪里。就好像我指定的尾随斜杠不知怎么被钉在了前面。我真的不明白这怎么可能

在Linux Ubuntu上使用g++4.5.2

非常感谢您的任何见解

事先非常感谢,

我想这行

//split1[0]是“主机:www.google.ie”

和你说的不一样。例如,如果您通过http获得它,您将

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

删除后的\n是

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

那卷毛是

fURL=“http://”+split2[1]+“/”;///\r/

这个


std::cout这是您的代码,放入一个小程序中,只需在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;
void foo(常量字符**split1)
{
std::string line=split1[0];//split1[0]是“主机:www.google.ie”
std::向量分裂2;
boost::split(split2,line,boost::是(“”)的任意值);
boost::erase_all(split2[1],“\n”);

std::coutWhat是
std::cout output的输出:“/URL:”(我看没问题:,代码中还有其他问题导致了这一点……例如,
fURL
的类型是什么?@Nim.Hmm。非常有趣。(ideone.com非常酷)。肯定还有其他原因……fURL的类型是“std::string”并在相应的头文件中声明。您是否尝试只使用发布的部分制作程序,并验证错误是否确实存在?之后是否输出更多内容?@Eamorr:这可以通过在拆分2[1]之间打印内容轻松测试还有std::endlImmilewsk-那真的很有帮助。谢谢。我想我现在已经查清楚了。。。
std::cout<<"fURL:"<<fURL<<std::endl
#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;
}