这里是部分我的代码:把字符串转换为矢量赋予空字符串
#include <stdio.h>
#include<string>
#include<string.h>
#include<algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main(){
FILE *in=fopen("C.in","r");
//freopen("C.out","w",stdout);
int maxl=0;
int i;
string word;
vector<string> words;
while(!feof(in)){
fscanf(in,"%s ",word.c_str());
int t=strlen(word.c_str());
if(t>maxl){
maxl=t;
words.clear();
words.insert(words.end(),word);
}else if (t==maxl){
words.insert(words.end(),word);
}
}
问题出现的
words.insert(words.end,word)
而
word
包含我的文件,矢量字item
words[i]
包含一个空字符串。
这怎么可能?
请显示您所有的变量声明! – OldProgrammer
word.c_str()返回'const char *',所以你不应该修改它。 – hetepeperfan
['while(!eof())'wrong。](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – chris