本文整理汇总了C++中SparseArray::push_back方法的典型用法代码示例。如果您正苦于以下问题:C++ SparseArray::push_back方法的具体用法?C++ SparseArray::push_back怎么用?C++ SparseArray::push_back使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SparseArray
的用法示例。
在下文中一共展示了SparseArray::push_back方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: readTopicFile
/**
* Read a topic vector file
* The format of these files are such that each line is:
* idx1 idx2 ... idxN ||| val1 val2 ... valN
* Which represents a coordinate form sparse array
* @param t Vector to write to
* @param fname The file name to read from
*/
void readTopicFile(vector<SparseArray>& t, const char *fname) {
cerr << "Reading " << fname << endl;
int linesRead = 1;
ifstream topic1file(fname,ifstream::in);
if(topic1file.fail()) {
cerr << "Failed to read " << fname << endl;
return;
}
string line;
while(getline(topic1file,line)) {
stringstream ss(line);
ss.exceptions(ifstream::failbit);
if(linesRead % 10 == 0) {
cerr << ".";
}
cerr.flush();
linesRead++;
SparseArray arr;
while(ss.peek() != '|') {
SparseArrayElem e;
ss >> e.idx;
arr.push_back(e);
while(ss.peek() == ' ') {
ss.get();
}
}
while(ss.peek() == '|' || ss.peek() == ' ') {
ss.get();
}
unsigned vals = 0;
for(auto it = arr.begin(); it != arr.end(); ++it) {
if(++vals > arr.size()) {
throw runtime_error("Index and value length differ (too many values)");
}
ss >> it->value;
}
if(vals != arr.size()) {
throw runtime_error("Index and value length differ (too few values)");
}
sort(arr.begin(),arr.end());
t.push_back(arr);
}
cerr << "OK" << endl;
}