C++ 如何将数组指针传递给函数 #包括 #包括 #包括 #包括 #包括 使用名称空间std; typedef矢量记录; typedef矢量数据; 整型排序(整型*数据,整型最大记录大小) { } int main() { //这是我们想要的数据。 数据; //这是包含数据的文件。请将其读入数据。 ifstream infle(“sort.txt”); 填充>>数据; //如果出了问题就抱怨。 如果(!infle.eof()) { cout
你没有数组。C(或C++)中的数组只是一个整数列表,你可以像传递一样传递它 然而,你有一个向量(我猜记录最后是一个int)。向量的行为很像数组,但它们不是,它们是实际的对象 您可能想做的是将函数编写为C++ 如何将数组指针传递给函数 #包括 #包括 #包括 #包括 #包括 使用名称空间std; typedef矢量记录; typedef矢量数据; 整型排序(整型*数据,整型最大记录大小) { } int main() { //这是我们想要的数据。 数据; //这是包含数据的文件。请将其读入数据。 ifstream infle(“sort.txt”); 填充>>数据; //如果出了问题就抱怨。 如果(!infle.eof()) { cout,c++,C++,你没有数组。C(或C++)中的数组只是一个整数列表,你可以像传递一样传递它 然而,你有一个向量(我猜记录最后是一个int)。向量的行为很像数组,但它们不是,它们是实际的对象 您可能想做的是将函数编写为 #include <fstream> #include <iostream> #include <sstream> #include <string> #include <vector> using namespace std; t
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
typedef vector <double> record_t;
typedef vector <record_t> data_t;
int sorted(int *data,int max_record_size)
{
}
int main()
{
// Here is the data we want.
data_t data;
// Here is the file containing the data. Read it into data.
ifstream infile( "sort.txt" );
infile >> data;
// Complain if something went wrong.
if (!infile.eof())
{
cout << "Fooey!\n";
return 1;
}
infile.close();
// Otherwise, list some basic information about the file.
cout << "Your CSV file contains " << data.size() << " records.\n";
unsigned max_record_size = 0;
for (unsigned n = 0; n < data.size(); n++)
if (max_record_size < data[ n ].size())
max_record_size = data[ n ].size();
cout << "The largest record has " << max_record_size << " fields.\n";
int i;
for (i=0; i <= max_record_size; i++)
{
cout << "your data contains " << data[ 0 ][ i ] << ".\n";
int temp[max_record_size];
sorted(&data,max_record_size);
//cout << "Your sorted data contains" << sorted [0] [i] << ".\n";
}
cout << "Good bye!\n";
system("PAUSE");
return 0;
}
你的电话是
int sorted(data_t& data, int max_record_size)
data\u t
是一个向量
。它甚至不接近整数数组。您只需要编写一个函数来处理数据而不是整数
如果函数应该对数据进行排序
,那么您应该使用std::sort
,要做到这一点,您需要编写一个比较函数,该函数可以比较数据的两个元素
,以便查看排序结果中哪一个元素应该在另一个元素之前
下面是一个使用lambda和字典编纂的_compare提供这种比较函数的示例
sorted(data,max_record_size);
尝试将引用传递给原始向量。为什么
- 您仍然希望使用原始向量而不制作副本
- 无法传递
引用,因此参数始终有效null
- 您没有执行任何指针运算,因此请注意安全并使用
参考
sort(begin(data),end(data), [](record_t const &lhs,record_t const &rhs) { return lexicographical_compare(begin(lhs),end(lhs),begin(rhs),end(rhs));] });
数据\u t
结构:
int sorted(data_t& data, int max_record_size)
{
}
现在您可以访问sorted
函数中的数据结构
同时
你知道你的代码不会编译吗
和unsigned max\u record\u size
无效。如果要在堆栈上分配数组,则需要使用常量大小int temp[max\u record\u size]
- 处理向量
的
类中的istream
运算符没有重载,因此此语句也被破坏:>
infle>>data;
数据作为int*
。如果你确定它们是相同的数据类型,请进行转换。数据是int
的列表?我不这么认为。不,需要的是pas的比较函数s到std::sort,以及.begin()和.end()迭代器。代码遵循并读取文本文件并将其存储在数据中。因此,我可以遍历整个列表,但我想将指向向量的指针传递给其他函数。我尝试了int-sorted(vector&data,int-max\u-record\u-size)但是这给了我一个错误。你必须有一个很棒的编译器。你可以将你的数据\u t
传递给任何函数,查看我的更改。谢谢你的工作!你能解释一下为什么函数原型将数据和数据作为一个参数吗?我对此不是很清楚。你应该阅读更多关于指针和引用的内容。查看我对st的编辑我很高兴这个解决方案适合你。
sorted(data, max_record_size);