Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

What is the output of this sort function?

Posted on 2008-06-10
5
Medium Priority
?
210 Views
Last Modified: 2010-04-21
IF
vector<int>   A;   // 10 elements, say 0-9
vector<long>   B;  // 10   elements, say all == 1
AND
struct SPredicate
{
      const long* _rowBegin;
      SPredicate(const vector<long>& v) : _rowBegin( v.begin() ) { }
      bool operator()(int i, int j) { return _rowBegin[ i ]  <  _rowBegin[ j ] ; }
};
What would the ouput be of the following?
std::sort(A.begin(), A.end(),SPredicate(B));


0
Comment
Question by:CTPAS
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 21751489
The predicate is indexing B and comparing B using the current value of i and j (both of which will be values of A); however, all elements in B are 1 so I'd expect that A would remain in the same order as it was initially.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21751551
^^^this assumes that the iterator of a vector is convertible to a type T *, which the C++ Standard doesn't as far as I can remember, define.
0
 

Author Comment

by:CTPAS
ID: 21751616
Thanks for quick resonse
0
 
LVL 40

Accepted Solution

by:
evilrix earned 500 total points
ID: 21751737
I believe this example code implements what your Q asks, yes? The output is what I stated I'd expect in my initial response, "...I'd expect that A would remain in the same order as it was initially"

Do you understand why this is the case though?
#include <vector>
#include <algorithm>
#include <iostream>
 
typedef std::vector<int> intvec_t;
typedef std::vector<long> longvec_t;
 
struct SPredicate
{
	longvec_t::const_iterator _rowBegin; 
	SPredicate(longvec_t & v) : _rowBegin( v.begin() ) { } 
	bool operator()(int i, int j) { return _rowBegin[ i ]  <  _rowBegin[ j ] ; } 
};
 
 
int main()
{
	int  aA[] = {5,6,7,8,9,0,1,2,3,4};
	long aB[] = {1,1,1,1,1,1,1,1,1,1};
 
	intvec_t A(aA, (aA+10));
	longvec_t B(aB, (aB+10));
 
	std::sort(A.begin(), A.end(), SPredicate(B));
 
	std::copy(A.begin(), A.end(), std::ostream_iterator<int>(std::cout, " "));
	std::cout << std::endl;
 
	std::copy(B.begin(), B.end(), std::ostream_iterator<long>(std::cout, " "));
	std::cout << std::endl;
}

Open in new window

0
 

Author Closing Comment

by:CTPAS
ID: 31465747
yes. I was only unclear about where i , j came from in the comparison.
This method is a compact way to order set A elements based on the ordering of set B
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question