CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: vector sort

  1. #1
    Join Date
    May 2015
    Posts
    500

    vector sort

    Hello,

    Practicing my c++ .. and

    Code:
    #include <cmath>
    #include <cstdio>
    #include <vector>
    #include <iostream>
    #include <algorithm>
    #include <iterator>
    #include <sstream>
    using namespace std;
    
    
    int main() {
    
         vector<int> vect;
    
        int numVect(0);
    
        cin >> numVect;
    
        cin.ignore();
        string str;
        getline(cin, str);
    
        stringstream ss(str);
    
        copy(istream_iterator<int>(ss), istream_iterator<int>(), back_inserter(vect));
    
        sort(vect.begin(),vect.end());
        for_each(begin(vect), end(vect), [](int v) {cout << v; });
        /* Enter your code here. Read input from STDIN. Print output to STDOUT */
        return 0;
    }
    input is :
    5

    1 6 10 8 4

    and i m getting the o/p: at the practice website

    ~ no response on stdout ~

    Another similar program also giving such issue:
    Code:
    #include "stdafx.h"
    #include "iterator"
    #include "sstream"
    #include "iostream"
    #include "algorithm"
    #include "vector"
    
    using namespace std;
    
    int main()
    {
    	string str;
    	int remElem, remRanBeg, remRanEnd;
    	vector<int> vec;
    
    	getline(cin, str);
    
    	getline(cin, str);
    	cin >> remElem;
    	cin.ignore();
    
    	cin >> remRanBeg;
    	cin >> remRanEnd;
    
    	stringstream ss(str);
    
    	copy(istream_iterator<int>(ss), istream_iterator<int>(), back_inserter(vec));
    
    	vec.erase(vec.begin()+remElem-1);
    
    	vec.erase(vec.begin()+ remRanBeg-1, vec.begin()+ remRanEnd-1);
    
        return 0;
    }
    but is ok when i executed in visual studio !!!
    thanks a lot
    pdk
    Last edited by pdk5; October 14th, 2020 at 10:15 AM.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: vector sort

    [moved from Visual C++ Programming forum]
    Victor Nijegorodov

  3. #3
    Join Date
    May 2015
    Posts
    500

    Re: vector sort

    Thankyou Victor for moving to appropriate place

  4. #4
    Join Date
    Oct 2020
    Posts
    2

    Re: vector sort

    Code:
    #include <iostream>
    #include <algorithm>
    #include <vector>
    #include <iterator>
    
    int main()
    {
        using namespace std;
    
        vector<int> vec;
        int numEl = 0;
        do cin >> numEl; while(numEl <=0 );
    
        vec.reserve(numEl);
    
        for (int i = 0, tmp; i < numEl; ++i) {
            cin >> tmp;
            vec.push_back(tmp);
        }
    
        sort(vec.begin(), vec.end());
        copy(vec.begin(), vec.end(), ostream_iterator<int>(cout, "\t"));
    
        return EXIT_SUCCESS;
    }
    I have somewhat modified the original code which works fine.
    Please, rate my post if it was useful for you.
    Cheers.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: vector sort

    Yes, works OK with VS. What os/compiler are you using?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  6. #6
    Join Date
    May 2015
    Posts
    500

    Re: vector sort

    Thanks a lot Davit and kaud..

    I was trying to solve the practice c++ challenges on the hackerrank website. I tried with my visual studio and it works ok. But when i presented the same code there, it gives the above error !!!

    Also, im trying to improve my c++ skills, do you recommend any sites, i can refer to get some kind of c++ exercises to make myself more comfortable with hands on.... im just using the above site in between my work to brush up various topics..

    And this site especially kaud has been really helpful in verifying my code and giving inputs... Thanks a lot
    Last edited by pdk5; October 15th, 2020 at 09:09 AM.

  7. #7
    Join Date
    Oct 2020
    Posts
    2

    Re: vector sort

    Quote Originally Posted by 2kaud View Post
    Yes, works OK with VS. What os/compiler are you using?
    VS 2019 Enterprise, Windows 10 64 bit.

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