Reputation: 1491
#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;
}
cannot convert
data_t*' to
int*' for argument1' to
int sorted(int*, int)'
Im trying to pass the pointer of my data which should be an array containing my list of numbers to my sort function what exactly am I doing wrong and can you please explain in detail so I can understand it, thanks!
Upvotes: 0
Views: 157
Reputation: 18850
Try passing a reference
to the original vector. Why?
null
reference, so the argument is always validYou're not doing any pointer arithmetic, so be safe and use a reference
int sorted(data_t& data, int max_record_size)
{
}
Pass your data_t
structure like this:
sorted(data, max_record_size);
Now you have access to the data_t structure inside your sorted
function.
Also
You know that your code is not going to compile?
unsigned max_record_size
and int temp[max_record_size]
are invalid. If you're going to allocate an array on the stack, you need to use a constant size.>>
operator in the istream
class that handles vector<vector<double>>
, so this statement is broken as well: infile >> data;
Upvotes: 0
Reputation: 88155
data_t
is a vector<vector<double>>
. This is not even close to an array of ints. You just need to write a function that handles data_t and not ints.
If the function is supposed to sort data
then you should use std::sort
, and to do that you'll need to write a comparison function that can compare two elements of data
in order to see which one should come before the other in the sorted result.
Here's an example of providing such a comparison function using a lambda with lexicographical_compare.
sort(begin(data),end(data), [](record_t const &lhs,record_t const &rhs) {
return lexicographical_compare(begin(lhs),end(lhs),begin(rhs),end(rhs));]
});
Upvotes: 0
Reputation: 12496
You don't have an array. An array in C (or C++) would be just a list of integers, and you could pass it like you did.
However, you have a vector (and I'm guessing record_t ends up being an int). vector<>s behave a lot like arrays, but they are not, they are actual objects.
What you probably want to do is write your function as
int sorted(data_t& data, int max_record_size)
and your call simply as
sorted(data,max_record_size);
Upvotes: 1