CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Sep 2007
    Posts
    6

    sorting a vector

    hello i am trying to sort a vector from least to greatest



    vector<float> molarnum;

    .... data being added to the vector....


    sort(molarnum.begin(),molarnum.end());
    float smallest = molarnum[0];




    i am using #include <algorithm> to call upon to sort()
    i keep geting a runtime error when i try sorting my vector.
    does anybody know on how i can fix this or have a good function for sorting a vector ?

    if anybody needs the full source i can give it , i just didnt want to post it due to so many lines of code

    thank you michael

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: sorting a vector

    Instead of providing the "full source", provide the smallest and simplest compilable program that demonstrates the error.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: sorting a vector

    "a runtime error" isn't very descriptive. What you've posted is fine, so odds are some other part of your program is corrupting memory.
    Last edited by Lindley; January 19th, 2009 at 11:51 PM.

  4. #4
    Join Date
    Sep 2007
    Posts
    6

    Re: sorting a vector

    let me rephrase my question then
    i tried to aproach this program using arrays instead vector
    the problem is when i call the array i get this in my debuger

    that error C2057: expected constant expression


    #include <iostream>

    using namespace std;
    int main()
    {

    int numele=2;
    cin >> numele;
    int elenum = numele;
    float molarnum[elenum];


    }

    i tried using vectors to state this array is there a way i can fix this
    sorry that i was not clear

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: sorting a vector

    That is because arrays in C++ have a fixed size. You are trying to use variable length arrays, which do not exist in standard C++. Since you already tried with vectors, why not use them?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Sep 2007
    Posts
    6

    Re: sorting a vector

    lets just say vectors are a little new to me

    i am going to post my code


    where the initiation of the vector is on line 231

    i cant make a simple program because i dont know where i am going wrong
    Attached Files Attached Files

  7. #7
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: sorting a vector

    I thought you were using std::sort(). If you only want to find the minimum element, use std::min_element().
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  8. #8
    Join Date
    Sep 2007
    Posts
    6

    Re: sorting a vector

    can you help me on how to state that

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: sorting a vector

    An example using a vector (though the vector is unnecessary in this case since I have a fixed size array)
    Code:
    #include <vector>
    #include <algorithm>
    #include <iostream>
    
    int main()
    {
        using namespace std;
    
        // Input array for convenience of initialisation.
        float input[] = {3.14, 2.718, 1.414, 4.5};
    
        vector<float> molarnum(input, input + sizeof(input) / sizeof(input[0]));
    
        vector<float>::iterator min_iter = min_element(molarnum.begin(), molarnum.end());
        if (min_iter != molarnum.end())
        {
            cout << "Minimum: " << *min_iter << endl;
        }
        else
        {
            cout << "The vector is empty." << endl;
        }
    }
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  10. #10
    Join Date
    Nov 2003
    Posts
    1,405

    Re: sorting a vector

    Quote Originally Posted by laserlight View Post
    An example using a vector (though the vector is unnecessary in this case since I have a fixed size array)
    There's a new STL container called array coming up in the new C++ standard.

    http://www.codeguru.com/cpp/cpp/cpp_...le.php/c15257/

  11. #11
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: sorting a vector

    Quote Originally Posted by _uj
    There's a new STL container called array coming up in the new C++ standard.
    I was aware of that, but the point of the example was to use the vector, not the array, so my note is concerning the fact that the array is purely to make initialisation of the vector easier in the absence of the initialisation syntax to be introduced by C++0x, despite the fact that the array alone would otherwise suffice for an example.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  12. #12
    Join Date
    Nov 2003
    Posts
    1,405

    Re: sorting a vector

    Quote Originally Posted by laserlight View Post
    I was aware of that, but the point of the example was to use the vector, not the array, so my note is concerning the fact that the array is purely to make initialisation of the vector easier in the absence of the initialisation syntax to be introduced by C++0x, despite the fact that the array alone would otherwise suffice for an example.
    I just wanted to add some information.

    This new STL container called "array" will retire STL vector as the preferred replacement for C style arrays.
    Last edited by _uj; January 20th, 2009 at 12:11 PM.

  13. #13
    Join Date
    Aug 2005
    Location
    San Diego, CA
    Posts
    1,054

    Question Re: sorting a vector

    Quote Originally Posted by _uj View Post
    I just wanted to add some information.

    This new STL container called "array" will retire STL vector as the preferred replacement for C style arrays.
    That's fabulous. No offense, but how does that help us at the present time using compilers that do not implement the new standard?

  14. #14
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: sorting a vector

    Quote Originally Posted by _uj
    This new STL container called "array" will retire STL vector as the preferred replacement for C style arrays.
    Generally, yes. However, in view that michael1991j attempted to use variable length arrays in C++, I daresay that std::vector is more appropriate here.

    Quote Originally Posted by kempofighter
    That's fabulous. No offense, but how does that help us at the present time using compilers that do not implement the new standard?
    The array container is available in TR1, of which there are existing implementations. That said, we are detracting from the topic of this thread.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured