- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想让标有//THIS LINE SHOULD BE PRINTING 的行执行它的操作,即打印“同义词”和“反义词”之间的 int 值。
这是文本文件:
词典.txt
1 cute
2 hello
3 ugly
4 easy
5 difficult
6 tired
7 beautiful
synonyms
1 7
7 1
antonyms
1 3
3 1 7
4 5
5 4
7 3
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
using namespace std;
class WordInfo{
public:
WordInfo(){}
~WordInfo() {
}
int id() const {return myId;}
void readWords(istream &in)
{
in>>myId>>word;
}
void pushSynonyms (string synline, vector <WordInfo> wordInfoVector)
{
stringstream synstream(synline);
vector<int> synsAux;
int num;
while (synstream >> num) synsAux.push_back(num);
for (int i=0; i<synsAux.size(); i++){
cout<<synsAux[i]<<endl; //THIS LINE SHOULD BE PRINTING
}
}
void pushAntonyms (string antline, vector <WordInfo> wordInfoVector)
{
}
//--dictionary output function
void printWords (ostream &out)
{
out<<myId<< " "<<word;
}
//--equals operator for String
bool operator == (const string &aString)const
{
return word ==aString;
}
//--less than operator
bool operator <(const WordInfo &otherWordInfo) const
{ return word<otherWordInfo.word;}
//--more than operator
bool operator > (const WordInfo &otherWordInfo)const
{return word>otherWordInfo.word;}
private:
vector <int> mySynonyms;
vector <int> myAntonyms;
string word;
int myId;
};
//--Definition of input operator for WordInfo
istream & operator >>(istream &in, WordInfo &word)
{
word.readWords(in);
}
//--Definition of output operator
ostream & operator <<(ostream &out, WordInfo &word)
{
word.printWords(out);
}
int main() {
string wordFile;
cout<<"enter name of dictionary file: ";
getline (cin,wordFile);
ifstream inStream (wordFile.data());
if(!inStream.is_open())
{
cerr<<"cannot open "<<wordFile<<endl;
exit(1);
}
vector <WordInfo> wordVector;
WordInfo aword;
while (inStream >>aword && (!(aword=="synonyms")))
{
wordVector.push_back(aword);
}
int i=0;
while (i<wordVector.size()){
cout<<wordVector[i]<<endl;
i++;
}
vector <int> intVector;
string aLine; //suspect
// bad statement?
while (getline(inStream, aLine)&&(aLine!=("antonyms"))){
aword.pushSynonyms(aLine, wordVector);
}
system("PAUSE");
return 0;
}
最佳答案
问题似乎出在这里:
in>>myId>>word;
在“同义词”行中,myId
的提取失败并在流上设置了failbit
,这会导致以下提取也失败。在从流中提取更多元素(如单词“同义词”)之前,您必须重置错误控制状态:
in.clear();
关于C++ ifstream 失败,为什么这条线没有到达它应该去的地方?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/456357/
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
操作无法完成。 Places API 库中发生内部错误。如果您认为此错误代表错误,请使用我们社区和支持页面 (https://developers.google.com/places/support)
我正在尝试在我的项目中使用 google places,我将其设置在 fragment 中而不是 Activity 中,我的自动完成 fragment 在 fragment 中。但是,当我尝试搜索它时
我的目的是使用R来查询google api。 我有一个地址和名称列表(属于商店、餐馆等),我需要为每个地址和名称存储: “纬度”、“经度”、“业务类型” 我的想法是使用 google place ap
我正在寻找设置一个自动完成的谷歌地方小部件。 我有一个带有“searchFieldText”id 的输入类型文本。 这是我的 JS 代码: var inputsec = document.getEle
是否可以使用图形 API(或地址/ zip )按纬度/经度和半径获取地点?我在文档中的任何地方都看不到它 最佳答案 搜索 URL 的以下格式将返回某个位置附近的地点列表: https://graph.
我正在探索 Google API,主要是 Places API。由于对 Google Places API 的请求数限制为 100,000,因此我正在寻找方法来最大限度地减少发送到 API 的请求数。
伙计们,我在我的应用程序中有一个功能,可以使用 GetFiles 在特定目录中搜索特定文件。方法 System.IO.Directory.GetFiles(string path, string
我已经在 Laravel 5.3 上使用 where 查询成功创建了许多函数,但是这次发生了一些奇怪的事情。 public function show($id){ $artikel = Art
我正在为我的 iPhone 应用程序使用 Facebook 图形 API 来获取附近地点的列表,我使用带有一些参数的“搜索”请求。我得到的响应是一个包含以下信息的地点列表:“纬度”、“经度”、“名称”
我有一个 Android 应用程序,我在其中使用 Google map 显示附近的地方,如加油站、药店等。我正在使用 map 和地点 API。 https://maps.googleapis.com/
我是一名优秀的程序员,十分优秀!