0
#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*' toint*' for argument 1' toint 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!

3
  • 3
    your indentation is so ridiculous I'm not even going to try to fix it. please clean up your code block. Commented Jul 23, 2012 at 21:38
  • You're trying to use a data_t* as an int*. If you're sure they are the same data type, do a cast. Commented Jul 23, 2012 at 21:38
  • data is a list of ints? I don't think so. Commented Jul 23, 2012 at 21:38

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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));]
});

1 Comment

Nah, what's needed is a comparison function to pass to std::sort, along with the .begin() and .end() iterators.
0

Try passing a reference to the original vector. Why?

  • You still want to work with the original vector without making a copy
  • You can't pass a null reference, so the argument is always valid
  • You'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.
  • There is no overload on the >> operator in the istream class that handles vector<vector<double>>, so this statement is broken as well: infile >> data;

4 Comments

The code complies and reads in the text files and stores it in data. So I can iterate through the entire list but I want to pass a pointer to the vector to a different function. I tried int sorted(vector<int> &data, int max_record_size) but it's giving me errors
You must have an awesome compiler. You can pass your data_t to any function, see my changes.
thanks that works! can you explain why the function prototype takes data_t& data as a parameter I'm not very clear on this.
You should read more about pointer and references. See my edits to start. I'm glad this solution works for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.