每当我将string
和vector
放在同一行时,字符串就会变空。
在我的代码中,我有,
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;
}
答案 0 :(得分: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%与你的“约束”问题一起工作。