【问题标题】:cout string and vector in the same linecout 字符串和向量在同一行
【发布时间】:2013-11-02 13:25:07
【问题描述】:

每当我将stringvector 放在同一行时,字符串就会变为空。 在我的代码中,

string line, s1, s2; 
vector<pair<string,int> > binaryvector;
ifstream filename(uncompr_filename.c_str());

如果我这样做

while(getline(filename, line))
{
    s1 = line.c_str();
    s2 = binaryvector[0].first.c_str();
    cout << s1 << endl;
    cout << s2 << endl;
}

它同时打印 s1 和 s2 的值。 但如果我这样做,

while(getline(filename, line))
{
    s1 = line.c_str();
    s2 = binaryvector[0].first.c_str();
    cout << s1 << s2 << endl;
}

它只打印 s2 字符串。我哪里错了? 这只是为了说明问题。我实际上想要做的是使用 if(s1 == s2) 比较 s1 和 s2。但如果我这样做,它会返回 false,因为 s1 似乎没有任何内容并且不等于 s2 字符串。

完整的代码。

#include <iostream>
#include <fstream>
#include <algorithm>
#include <string>
#include <map>
#include <vector>
#define DICTIONARYSIZE 4

using namespace std;
typedef map<string,int> Instruction_Binaries;
struct val_greaterthan : binary_function < pair<string,int>, pair<string,int>, bool >
{
        bool operator() (const pair<string,int>& x, const pair<string,int>& y) const
        {return x.second > y.second;}
}val_gt;

int main(int argc, char *argv[])
{
    int c, i;
    Instruction_Binaries binary_count;
    string uncompr_filename, compr_filename, outfilename;

        if(argc <= 3)
        {
                cout << "Format is \"./SIM -c original.txt cout.txt\" or \"./SIM -d compressed.txt dout.txt\"" << endl;
                return 1;
        }
    while ((c = getopt (argc, argv, "c:d:")) != -1)
        switch (c)
        {
                case 'c':
                        (uncompr_filename=optarg);
                        break;
                case 'd':
                        (compr_filename=optarg);
                        break;
                default:
                        cout << "Format is \"./SIM -c original.txt cout.txt\" or \"./SIM -d compressed.txt dout.txt\"" << endl;
                        abort ();
                        break;
        }

    ifstream ifile(uncompr_filename.c_str());
    string binary, Directory_Index;

    while (ifile >> binary){
        int index;
        ++binary_count[binary];
    }

    vector<pair<string,int> > binaryvector;
    copy(binary_count.begin(), binary_count.end(), back_inserter(binaryvector));
    sort(binaryvector.begin(), binaryvector.end(), val_gt);

    while(ifile >> binary){
        int flag = 0;
        for(i=0; i<4; ++i){
                if(i==0) Directory_Index = "00";
                else if(i==1) Directory_Index = "01";
                else if(i==2) Directory_Index = "10";
                else if(i==3) Directory_Index = "11";
                if(binary == binaryvector[i].first){
                        cout << "001" << Directory_Index << endl;
                        flag=1;
                        break;
                }
        }
    if(flag == 0)
                cout << binary << endl;
    }
    return 0;
}

【问题讨论】:

  • 我假设 binaryvector 在其他地方初始化?另外,为什么将 s2 设置为等于 binaryvector[0].first.c_str() 而不仅仅是 binaryvector[0].first。 (你确定 s2 不包含像 \r 这样的东西作为第一个字符吗?这可能会将回车返回到行首,清除 s1,但在换行符之后,它是不可见的。)
  • 是的,对不起,我没有放完整的代码。我会更新问题
  • 如果你这样做cout &lt;&lt; s1; cout &lt;&lt; s2 &lt;&lt; endl;怎么办?
  • 即使这样它也只打印 s2 的值
  • @Charlie,我只尝试了 binaryvector[0].first。我看到了这个问题,所以我想他们的类型可能有所不同。在没有完全理解的情况下尝试黑客攻击:|对 C++ 非常陌生

标签: c++ string vector cout


【解决方案1】:

问题出在代码中的其他地方,因为它有效:

#include <iostream>
#include <vector>
#include <string>
#include <utility>
#include <fstream>

using namespace std;

int main()
{
    string line, s1, s2; 
    vector<pair<string,int> > binaryvector;
    binaryvector.push_back({"heya", 5});
    ifstream filename("input.txt");

    while(getline(filename, line))
    {
        s1 = line.c_str();
        s2 = binaryvector[0].first.c_str();
        cout << s1 << s2 << endl;
    }
    return 0;
}

我在http://www.compileonline.com/compile_cpp11_online.php 上运行了这个,输入文件如下所示:

This is the file you can use to provide input to your program and later on open it inside your program to process the input.
second line
third line

After a blank line!  Booya!
and a line without "ending" it

输出如下所示:

This is the file you can use to provide input to your program and later on open it inside your program to process the input.
heya
second line
heya
third line
heya
heya
After a blank line! Booya!
heya
and a line without "ending" itheya

这意味着cout 没有问题,但你在某处做错了其他可怕的事情,因为我刚刚制作的“最小”程序 100% 可以解决你的“受限”问题。

【讨论】:

  • 哦,那行得通..但这却没有。 if(s1 == s2) cout
  • 抱歉评论编辑不佳。还是习惯格式
  • 凯文,感谢您的帮助。找出问题所在。 stackoverflow.com/questions/2310939/…
猜你喜欢
  • 1970-01-01
  • 2016-12-10
  • 1970-01-01
  • 1970-01-01
  • 2019-06-03
  • 2014-12-11
  • 2015-12-30
  • 1970-01-01
  • 2017-02-28
相关资源
最近更新 更多