How would I add a line to the main function to sort the people vector by Age using the STL sort function?
#include <iostream> #include <algorithm> #include <vector> #include <string> using namespace std; struct Person { string name; int age; string favoriteColor; }; bool sortByName(Person &lhs, Person &rhs) { return lhs.name < rhs.name; } bool sortByAge(Person &lhs, Person &rhs) { return lhs.age < rhs.age; } bool sortByColor(Person &lhs, Person &rhs) { return lhs.favoriteColor < rhs.favoriteColor; } int main() { vector<Person> people(5); for (int i = 0; i< 5; i++) { cout << "Person #" << i + 1 << " name: "; cin >> people[i].name; cout << "Person #" << i + 1 << " age: "; cin >> people[i].age; cout << "Person #" << i + 1 << " favorite color: "; cin >> people[i].favoriteColor; } //call STL sort function here! }
**To sort a vector array using STL sort function with our own function then this line of statement is to used.
std::sort(vector_name.begin(), vector_name.end(), compareByLength);
*For sort your data using length the statement will be:
std::sort(people.begin(), people.end(), sortByAge);
*Added some small piece of code to your code to enter a choice and sort based on it:
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
struct Person
{
string name;
int age;
string favoriteColor;
};
bool sortByName(Person &lhs, Person &rhs)
{
return lhs.name < rhs.name;
}
bool sortByAge(Person &lhs, Person &rhs)
{
return lhs.age < rhs.age;
}
bool sortByColor(Person &lhs, Person &rhs)
{
return lhs.favoriteColor < rhs.favoriteColor;
}
int main()
{
vector<Person> people(5);
for (int i = 0; i< 5; i++)
{
cout << "Person #" << i + 1 << " name: ";
cin >> people[i].name;
cout << "Person #" << i + 1 << " age: ";
cin >> people[i].age;
cout << "Person #" << i + 1 << " favorite color:
";
cin >> people[i].favoriteColor;
}
int choice;
cout<<"Enter your choice to sort.."<<endl;
cout<<"1.By Name 2.By Age 3.Color"<<endl;
cin>>choice;
if(choice==1)
{
cout<<"Sorting by Name"<<endl;
std::sort(people.begin(), people.end(), sortByName);
}
else if(choice==2)
{
cout<<"Sorting by Age"<<endl;
std::sort(people.begin(), people.end(), sortByAge);
}
else
{
cout<<"Sorting by Color"<<endl;
std::sort(people.begin(), people.end(), sortByColor);
}
for (int i = 0; i< 5; i++)
{
cout << people[i].name<<" ";
cout << people[i].age<<" ";
cout << people[i].favoriteColor<<" ";
cout<<endl;
}
}
How would I add a line to the main function to sort the people vector by...
This is a c++ code /** Write a function sort that sorts the elements of a std::list without using std::list::sort or std::vector. Instead use list<int>::iterator(s) */ using namespace std; #include <iostream> #include <iomanip> #include <string> #include <list> void print_forward (list<int>& l) { // Print forward for (auto value : l) { cout << value << ' '; } cout << endl; } void sort(list<int>& l) { write code here -- do not use the built-in list sort function }...
Fix the function so that it checks if the string is a palindrome #include <iostream> using namespace std; //Fix the function so that it checks if the string is a palindrome //(same forwards as backwards ex: racecar / radar) bool is_palindrome(string str, int i){ //base cases if (/* add the condition */) return false; if (/* add the condition */) return true; //recursive call return is_palindrome(str, i-1); } int main(){ string x; cin >> x; if (is_palindrome(x,x.length())){...
So Im here and im stuck. I need to figure out how to get the character of the first name to be printed on a separate line and the middle and last. is there a way where i can tell a my function to stop once it hit " "? Please help. I #include iostream 2 #include string using namespace i; string name 7 getline cin, name 4 int main ) cout<"Please enter your full name: " problem3.cpp 3Write a...
10.18 LAB: Plant information (vector) Given a base Plant class and a derived Flower class, complete main() to create a vector called myGarden. The vector should be able to store objects that belong to the Plant class or the Flower class. Create a function called PrintVector(), that uses the PrintInfo() functions defined in the respective classes and prints each element in myGarden. The program should read plants or flowers from input (ending with -1), adding each Plant or Flower to...
test.txtLab7.pdfHeres the main.cpp:#include "Widget.h"#include <vector>#include <iostream>#include <string>#include <iomanip>#include <fstream>using std::ifstream;using std::cout;using std::cin;using std::endl;using std::vector;using std::string;using std::setprecision;using std::setw;bool getWidget(ifstream& is, Widget& widget){ string name; int count; float unitCost; if (is.eof()) { return false; } is >> count; is >> unitCost; if (is.fail()) { return false; } is.ignore(); if (!getline(is, name)) { return false; } widget.setName(name); widget.setCount(count); widget.setUnitCost(unitCost); return true;}// place the definitions for other functions here// definition for function mainint main(){ // Declare the variables for main here // Prompt the...
First create the two text file given below. Then complete the main that is given. There are comments to help you. An output is also given You can assume that the file has numbers in it Create this text file: data.txt (remember blank line at end) Mickey 90 Minnie 85 Goofy 70 Pluto 75 Daisy 63 Donald 80 Create this text file: data0.txt (remember blank line at end) PeterPan 18 Wendy 32 Michael 28 John 21 Nana 12 Main #include...
This is C++ I only can change the lines highlighted in white (i can add lines to it with enter though) Set hasDigit to true if the 3-character passCode contains a digit. 1 #include <iostream» 2 #include <string> 3 #include <cctype> 4 using namespace std; 6 int main 7 bool hasDigit; 8 string passCode; 10 hasDigit false; 11 cin passCode; 12 13 Your solution goes here 14 15 if ChasDigit) 16 cout << "Has a digit." < endl; 17 18...
#include <iostream> #include <string> using namespace std; //Write a function that changes all characters in a string to dashes string to_dash(string s){ for(int i = 0; i < s.length(); i++){ } return s; } int main(){ string s; cin >> s; s = to_dash(s); cout << s << endl; }
IN C++ PLEASE -------Add code to sort the bowlers. You have to sort their parallel data also. Print the sorted bowlers and all their info . You can use a bubble sort or a shell sort. Make sure to adjust your code depending on whether or not you put data starting in row zero or row one. Sort by Average across, lowest to highest. The highest average should then be on the last row.. When you sort the average, you...
Add binary_search() (page 462) to your program. Modify main() to prompt the user for a number to search (until ^D) and display the position of the number in the sorted vector. Try your program for the following user input: 1 15 18 40 30 50 ^D The output should be: -1 2 -1 7 5 -1 int binary_search(vector<int> v, int from, int to, int value) { if (from > to) return -1; int mid = (from + to) / 2;...