use pointers instead

This commit is contained in:
Krishna Vedala 2020-05-28 21:30:42 -04:00
parent 8291db4f9f
commit ecddfd2704
No known key found for this signature in database
GPG Key ID: BA19ACF8FC8792F7

View File

@ -14,16 +14,16 @@
* @todo add documentation * @todo add documentation
*/ */
template <class X> template <class X>
void comp(X x, std::vector<int> &s1, std::vector<int> &s2, void comp(X x, std::vector<int> *s1, std::vector<int> *s2,
std::vector<int> &s3) { std::vector<int> *s3) {
if (s1.size() >= x && s1.size() + s2.size() < x) { if (s1->size() >= x && s1->size() + s2->size() < x) {
std::cout << s2[0] << " is the " << x + 1 << "th element from front"; std::cout << (*s2)[0] << " is the " << x + 1 << "th element from front";
} else if (s1.size() > x) { } else if (s1->size() > x) {
std::sort(s1.begin(), s1.end()); std::sort(s1->begin(), s1->end());
std::cout << s1[x] << " is the " << x + 1 << "th element from front"; std::cout << (*s1)[x] << " is the " << x + 1 << "th element from front";
} else if (s1.size() + s2.size() <= x && s3.size() > x) { } else if (s1->size() + s2->size() <= x && s3->size() > x) {
std::sort(s3.begin(), s3.end()); std::sort(s3->begin(), s3->end());
std::cout << s3[x - s1.size() - s2.size()] << " is the " << x + 1 std::cout << (*s3)[x - s1->size() - s2->size()] << " is the " << x + 1
<< "th element from front"; << "th element from front";
} else { } else {
std::cout << x + 1 << " is invalid location"; std::cout << x + 1 << " is invalid location";
@ -71,7 +71,7 @@ int main() {
int x; int x;
std::cout << "enter the no. to be searched form begining:- "; std::cout << "enter the no. to be searched form begining:- ";
std::cin >> x; std::cin >> x;
comp(x - 1, s1, s2, s3); comp(x - 1, &s1, &s2, &s3);
return 0; return 0;
} }