注册 登录
编程论坛 C++教室

为什么会有一段空白在输出字符串前面?

ClarenceC 发布于 2014-03-08 19:01, 481 次点击
fstream executed;
    string file_name("D:\d.txt"); //content is "hello nihao zhongxiaogang clarence hello world"
    executed.open(file_name.c_str());
    vector<string> executed_vec(10);
    set<string> iset;
    string word;
    while(executed>>word)
    {
        executed_vec.push_back(word);
        iset.insert(word);
    }
    for(vector<string>::const_iterator vecp=executed_vec.begin();vecp!=executed_vec.end();++vecp)
    {
        cout<<*vecp<<" ";//why this consequence has a blank stretch in front of string
    }
    cout<<endl;
    for(set<string>::const_iterator setp=iset.begin();setp!=iset.end();++setp)
    {
        cout<<*setp<<" ";//why this has no blank stretch
    }
    cout<<endl;
    return 0;
结果是:[local]1[/local]  为什么会有一段空白在第一排字符串之前呢?
不好意思各位,我不知道怎么没法加上图片。我描述一下:在控制台输出结果中会有一段空白在“hello nihao zhongxiaogang clarence hello world"前,
如   "          (前面是一段空白)hello nihao zhongxiaogang clarence hello world"

[ 本帖最后由 ClarenceC 于 2014-3-8 19:06 编辑 ]
1 回复
#2
rjsp2014-03-10 09:04
你这代码瞎扯蛋的吧,比如"D:\d.txt",按理应该是"D:\\d.txt",我不信你用"D:\d.txt"能运行出结果。

关于空格,因为你的代码 vector<string> executed_vec(10); 呀,你预先就给它10个空字符串。自己写的东西都不知道,不会是抄来的吧
1