繁体   English   中英

cout字符串和向量在同一行

[英]cout string and vector in the same line

每当我将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;
}

问题在代码中的其他地方,因为它可以正常工作:

#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%起作用。

暂无
暂无

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

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