Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
按列对CSV文件进行排序,并与C++; 我有一些CSV文件,我可以导入到C++中,把它们分成列并打印出来,但是我无法执行我需要的分析。我希望能够对每个列进行排序(升序或降序),然后在1或0的单独列中找到分组。这是我到目前为止的代码,但似乎每次创建新行时都要替换变量 #include "stdafx.h" #include <iostream> #include <fstream> #include <vector> using namespace std; struct sampleData { //Create set of variables that are used in the vector //to print sampleData float first, second, third, fourth; }; void printSample(sampleData sample) // Function that prints the vectors. { cout << sample.first << " " << sample.second << " " << sample.third << " " << sample.fourth endl; } int main() { ifstream myFile("file.csv"); //Open file with ifstream constructor. if (myFile.is_open()) { vector<sampleData> sample; //Create vector that stores the variables declared in the struct. float first, second, third, fourth; char delim; while (myFile >> first >> delim >> second >> delim >> third >> delim >> fourth) { //Places each value of csv file into individual variables in the vector. sample.push_back({ first, second, third, fourth }); } cout << "First value " << " Second value: " << " Third Value: " << " Fourth Value: " << endl; //Create column headers. for (int x(0); x < sample.size(); ++x) { printSample(sample.at(x)); } } else { cout << "The file did not open."; //Let's me know if file has not been opened. } system("pause"); return 0; }_C++_Arrays_Csv_Sorting_Analysis - Fatal编程技术网

按列对CSV文件进行排序,并与C++; 我有一些CSV文件,我可以导入到C++中,把它们分成列并打印出来,但是我无法执行我需要的分析。我希望能够对每个列进行排序(升序或降序),然后在1或0的单独列中找到分组。这是我到目前为止的代码,但似乎每次创建新行时都要替换变量 #include "stdafx.h" #include <iostream> #include <fstream> #include <vector> using namespace std; struct sampleData { //Create set of variables that are used in the vector //to print sampleData float first, second, third, fourth; }; void printSample(sampleData sample) // Function that prints the vectors. { cout << sample.first << " " << sample.second << " " << sample.third << " " << sample.fourth endl; } int main() { ifstream myFile("file.csv"); //Open file with ifstream constructor. if (myFile.is_open()) { vector<sampleData> sample; //Create vector that stores the variables declared in the struct. float first, second, third, fourth; char delim; while (myFile >> first >> delim >> second >> delim >> third >> delim >> fourth) { //Places each value of csv file into individual variables in the vector. sample.push_back({ first, second, third, fourth }); } cout << "First value " << " Second value: " << " Third Value: " << " Fourth Value: " << endl; //Create column headers. for (int x(0); x < sample.size(); ++x) { printSample(sample.at(x)); } } else { cout << "The file did not open."; //Let's me know if file has not been opened. } system("pause"); return 0; }

按列对CSV文件进行排序,并与C++; 我有一些CSV文件,我可以导入到C++中,把它们分成列并打印出来,但是我无法执行我需要的分析。我希望能够对每个列进行排序(升序或降序),然后在1或0的单独列中找到分组。这是我到目前为止的代码,但似乎每次创建新行时都要替换变量 #include "stdafx.h" #include <iostream> #include <fstream> #include <vector> using namespace std; struct sampleData { //Create set of variables that are used in the vector //to print sampleData float first, second, third, fourth; }; void printSample(sampleData sample) // Function that prints the vectors. { cout << sample.first << " " << sample.second << " " << sample.third << " " << sample.fourth endl; } int main() { ifstream myFile("file.csv"); //Open file with ifstream constructor. if (myFile.is_open()) { vector<sampleData> sample; //Create vector that stores the variables declared in the struct. float first, second, third, fourth; char delim; while (myFile >> first >> delim >> second >> delim >> third >> delim >> fourth) { //Places each value of csv file into individual variables in the vector. sample.push_back({ first, second, third, fourth }); } cout << "First value " << " Second value: " << " Third Value: " << " Fourth Value: " << endl; //Create column headers. for (int x(0); x < sample.size(); ++x) { printSample(sample.at(x)); } } else { cout << "The file did not open."; //Let's me know if file has not been opened. } system("pause"); return 0; },c++,arrays,csv,sorting,analysis,C++,Arrays,Csv,Sorting,Analysis,您需要提供比较器函数,用于在排序过程中比较各个行。希望你有一个现代版本的C++,可以使用lambdas: // Sort by first column std::sort( samples.begin(), samples.end(), []( const sampleData& a, const sampleData& b ) { return a.first < b.first; } ); []是引入lambda的“捕获列表” (对不起,我还没

您需要提供比较器函数,用于在排序过程中比较各个行。希望你有一个现代版本的C++,可以使用lambdas:

// Sort by first column
std::sort( samples.begin(), samples.end(),
  []( const sampleData& a, const sampleData& b )
  {
    return a.first < b.first;
  }
);
[]
是引入lambda的“捕获列表”

(对不起,我还没有一个更好的世界常见问题解答…)


*大概吧。实际上,在幕后,这比这要复杂一点。

好的。太棒了。这使我能够根据需要对它们进行分类。尽管如此,我不确定samples.end()后面的“[]”是什么意思。这到底是做什么的?我不知道你在哪里看到一个“[]”,但是括号后面的括号是用来调用成员函数的…在引用常量“a”和“b”之前的开括号和闭括号。我认为这是一个链接,但我不是积极的。哦,你说的是lambda。这就是创建内联函数对象的语法。在这里,我将编辑答案。这是一个lambda!这是有道理的。通过链接,我了解了基本概念。我对编程很陌生,所以这是真正的艺术,对吧?我只是停留在这个迭代器上计算1和0。如果你不介意的话,有什么建议吗?
// Sort by first column
std::sort( samples.begin(), samples.end(),
  []( const sampleData& a, const sampleData& b )
  {
    return a.first < b.first;
  }
);
struct unnamed_function
{
  bool operator () ( const sampleData& a, const sampleData& b ) const
  {
    return a.first < b.first;
  }
};

...

std::sort( samples.begin(), samples.end(), unnamed_function() );