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 }...
/* Implementation of the main() method of the program. */ #include <iostream> #include <string> #include <vector> #include <algorithm> using namespace std; class Student { public: // default constructor Student() { studentID = 0; studentFirst = "First"; studentMiddle = "Middle"; studentLast = "Last"; } void SetStudentID(int number) { studentID = number; } void SetStudentFirst(string name) { studentFirst = name; } void SetStudentMiddle(string name) { studentMiddle =...
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...
#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; }
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...
How would I loop this so that I can test out the times 1730 and 0900 and the reverse, 0900 and 1730? Preferably a "do, while" loop if possible? #include <iostream> #include <string> using namespace std; int main() { string name; cout << "Please enter your last name " << endl; cin >> name; cout << "Welcome, " << name << endl; //Variables that are being declared int time1; int...