copy some elements of a vector in reverse order

Discussion in 'C++' started by tom, Aug 18, 2007.

  1. tom

    tom Guest

    Question:
    Give a vector that has 10 elements, copy the elements from position 3
    through 7 in reverse order to a list
    I have a solution in the below, but I'm feel there could be a better
    one, (just not quite confident with this one)

    My answer:
    ==========
    #include <iostream>
    #include <vector>
    #include <hash_map>
    #include <cctype>
    #include <cassert>
    #include <fstream>
    #include <sstream>
    #include <list>
    #include <deque>
    #include <algorithm>
    #include <numeric>
    #include <stack>
    #include <queue>
    #include <map>
    #include <set>

    using namespace std;
    using namespace stdext;

    int main(int argc, char *argv[])
    {
    vector<int> intVector;
    for(int i=0; i<10; i++)
    {
    intVector.push_back(i);
    }

    list<int> intList;
    for(int i=3; i<=7; ++i)
    {
    intList.push_front(intVector);
    }

    ostream_iterator<int> oiter(cout, " ");
    list<int>::const_iterator iter = intList.begin();
    while(iter!=intList.end())
    {
    *oiter++ = *iter++;
    }

    return 0;
    }
     
    tom, Aug 18, 2007
    #1
    1. Advertising

  2. tom

    Ondra Holub Guest

    On 18 Srp, 07:28, tom <> wrote:
    > Question:
    > Give a vector that has 10 elements, copy the elements from position 3
    > through 7 in reverse order to a list
    > I have a solution in the below, but I'm feel there could be a better
    > one, (just not quite confident with this one)
    >
    > My answer:
    > ==========
    > #include <iostream>
    > #include <vector>
    > #include <hash_map>
    > #include <cctype>
    > #include <cassert>
    > #include <fstream>
    > #include <sstream>
    > #include <list>
    > #include <deque>
    > #include <algorithm>
    > #include <numeric>
    > #include <stack>
    > #include <queue>
    > #include <map>
    > #include <set>
    >
    > using namespace std;
    > using namespace stdext;
    >
    > int main(int argc, char *argv[])
    > {
    > vector<int> intVector;
    > for(int i=0; i<10; i++)
    > {
    > intVector.push_back(i);
    > }
    >
    > list<int> intList;
    > for(int i=3; i<=7; ++i)
    > {
    > intList.push_front(intVector);
    > }
    >
    > ostream_iterator<int> oiter(cout, " ");
    > list<int>::const_iterator iter = intList.begin();
    > while(iter!=intList.end())
    > {
    > *oiter++ = *iter++;
    > }
    >
    > return 0;
    >
    > }


    copy(intVector.begin() + 3, intVector.begin() + 7 + 1,
    front_inserter(intList));
     
    Ondra Holub, Aug 18, 2007
    #2
    1. Advertising

  3. tom

    Ondra Holub Guest

    On 18 Srp, 07:28, tom <> wrote:
    > Question:
    > Give a vector that has 10 elements, copy the elements from position 3
    > through 7 in reverse order to a list
    > I have a solution in the below, but I'm feel there could be a better
    > one, (just not quite confident with this one)
    >
    > My answer:
    > ==========
    > #include <iostream>
    > #include <vector>
    > #include <hash_map>
    > #include <cctype>
    > #include <cassert>
    > #include <fstream>
    > #include <sstream>
    > #include <list>
    > #include <deque>
    > #include <algorithm>
    > #include <numeric>
    > #include <stack>
    > #include <queue>
    > #include <map>
    > #include <set>
    >
    > using namespace std;
    > using namespace stdext;
    >
    > int main(int argc, char *argv[])
    > {
    > vector<int> intVector;
    > for(int i=0; i<10; i++)
    > {
    > intVector.push_back(i);
    > }
    >
    > list<int> intList;
    > for(int i=3; i<=7; ++i)
    > {
    > intList.push_front(intVector);
    > }
    >
    > ostream_iterator<int> oiter(cout, " ");
    > list<int>::const_iterator iter = intList.begin();
    > while(iter!=intList.end())
    > {
    > *oiter++ = *iter++;
    > }
    >
    > return 0;
    >
    > }


    Or you can initialize list already in constructor, but you need
    convert forward iterators to reverse_iterators:

    list<int> intList(
    vector<int>::reverse_iterator(intVector.begin() + 7 + 1),
    vector<int>::reverse_iterator(intVector.begin() + 3)
    );
     
    Ondra Holub, Aug 18, 2007
    #3
  4. Hi!

    tom schrieb:
    > Question:
    > Give a vector that has 10 elements, copy the elements from position 3
    > through 7 in reverse order to a list


    Apart from Ondra's solutions: There is also a "copy_backward" algorithm:

    http://www.sgi.com/tech/stl/copy_backward.html

    Frank
     
    Frank Birbacher, Aug 18, 2007
    #4
  5. On 2007-08-18 07:28, tom wrote:
    > Question:
    > Give a vector that has 10 elements, copy the elements from position 3
    > through 7 in reverse order to a list
    > I have a solution in the below, but I'm feel there could be a better
    > one, (just not quite confident with this one)
    >
    > My answer:
    > ==========
    > #include <iostream>
    > #include <vector>
    > #include <hash_map>
    > #include <cctype>
    > #include <cassert>
    > #include <fstream>
    > #include <sstream>
    > #include <list>
    > #include <deque>
    > #include <algorithm>
    > #include <numeric>
    > #include <stack>
    > #include <queue>
    > #include <map>
    > #include <set>


    Do you always include all those headers, or did you post a part of some
    bigger piece of code and forgot to delete the unused ones?

    --
    Erik Wikström
     
    =?ISO-8859-1?Q?Erik_Wikstr=F6m?=, Aug 18, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dogbite
    Replies:
    4
    Views:
    701
    osmium
    Oct 10, 2003
  2. Alex
    Replies:
    2
    Views:
    1,254
  3. Replies:
    8
    Views:
    1,962
    Csaba
    Feb 18, 2006
  4. John
    Replies:
    2
    Views:
    725
    Ivan Vecerina
    Apr 3, 2006
  5. Replies:
    26
    Views:
    2,140
    Roland Pibinger
    Sep 1, 2006
Loading...

Share This Page