copy some elements of a vector in reverse order

T

tom

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;
}
 
O

Ondra Holub

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));
 
O

Ondra Holub

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)
);
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top