Question

C++ Question about Struct and Vector) Item Number format : box a11b2 struct itemNumber {   ...

C++ Question about Struct and Vector)

Item Number format : box a11b2


struct itemNumber {
   string name; // Item name

char a, b; // Numbers
int num1, num2; // Numbers
}

1. How do I put struct in vector?

2. How do I sort them by comparing num in vector?

ex) (in this way)

a1b1 box1

a1b5 box2

a2b3 box3

a5b1 box4

...

3. How do I change the name of item in the structure in the vector when item numbers are the same?

ex)

a1b1 box1

a1b1 box2

=========== change to =====

a1b1 box1 box2

Homework Answers

Answer #1

Hi, please find the solution and rate the answer.

//

// main.cpp

// StructVector

//

// Created by Abhirama Gopala Dasa on 14/09/19.

// Copyright © 2019 Abhirama Gopala Dasa. All rights reserved.

//

#include <iostream>

#include "string"

#include "vector"

using namespace std;

struct itemNumber {

string name; // Item name

  

char a, b; // Numbers

int num1, num2; // Numbers

string toString(){

string s = "Name:";

s.append(name);

s.append("\n");

s.append("a:");

s.append(to_string(num1));

s.append("\n");

  

s.append("b:");

s.append(to_string(num2));

s.append("\n");

return s;

}

};

bool comparisonItemNumber(struct itemNumber item1,struct itemNumber item2){

string s1,s2;//change this function to change the way sorting happens.

  

s1 = to_string(item1.a)

.append(to_string(item1.num1))

.append(to_string(item1.b))

.append(to_string(item1.num2))

.append(" ")

.append(item1.name);

s2 =to_string(item2.a)

.append(to_string(item2.num1))

.append(to_string(item2.b))

.append(to_string(item2.num2))

.append(" ")

.append(item2.name);

//now s1 will be like "a1b1 box1" and s2 like "a1b5 box2" and a1b1 comes before a1b5 and will be sorted like that

//comparing both below with compare function availabel in string

return s1.compare(s2)<0;

}

int main(int argc, const char * argv[]) {

vector<struct itemNumber> vect;

struct itemNumber item,item1,item2,item3,item4,item5;

item.name = "box1";

item1.name = "box2";

item2.name = "box3";

item3.name = "box4";

item4.name = "box5";

item.a = 'a';item.b='b';

item1.a = 'a';item1.b='b';

item2.a = 'a';item2.b='b';

item3.a = 'a';item3.b='b';

item4.a = 'a';item4.b='b';

item.num1 = rand()%10+1;item.num2 = rand()%10+1;

item1.num2 = rand()%10+1;item1.num2 = rand()%10+1;

item2.num1 = rand()%10+1;item2.num2 = rand()%10+1;

item3.num1 = rand()%10+1;item3.num2 = rand()%10+1;

item4.num1 = rand()%10+1;item4.num2 = rand()%10+1;

vect.push_back(item);

vect.push_back(item1);

vect.push_back(item2);

vect.push_back(item3);

vect.push_back(item4);

cout<<"print before sort"<<endl;

for (auto x : vect)

cout <<x.toString();

cout<<endl<<"print after sort"<<endl;

sort(vect.begin(), vect.end(), comparisonItemNumber);

for (auto x : vect)

cout <<x.toString();

  

//now we add

item5.a='a';item5.b='b';

item5.num1 = item2.num1;

item5.num2 = item2.num2;

item5.name = "box5";

vect.push_back(item5);

sort(vect.begin(), vect.end(), comparisonItemNumber);

cout<<"\nAfter adding and sorting\n";

for (auto x : vect)

cout <<x.toString();

cout<<"\nbefore erasing \n";

for (int i=1; i<vect.size(); i++) {

string s = "a";

s.append(to_string(vect[i-1].num1));

s.append("b");

s.append(to_string(vect[i-1].num2));

string s1 = "a";

s1.append(to_string(vect[i].num1));

s1.append("b");

s1.append(to_string(vect[i].num2));

if(s.compare(s1)==0){

string ss = " ";

ss.append(vect[i].name);

vect[i-1].name.append(ss);

vect.erase(vect.begin()+i);

}

}

cout<<"\n after erasing\n";

for (auto x : vect)

cout <<x.toString();

}

Sample Output:

print before sort

Name:box1

a:8

b:10

Name:box2

a:0

b:9

Name:box3

a:1

b:3

Name:box4

a:5

b:9

Name:box5

a:4

b:10

print after sort

Name:box2

a:0

b:9

Name:box3

a:1

b:3

Name:box5

a:4

b:10

Name:box4

a:5

b:9

Name:box1

a:8

b:10

After adding and sorting

Name:box2

a:0

b:9

Name:box3

a:1

b:3

Name:box5

a:1

b:3

Name:box5

a:4

b:10

Name:box4

a:5

b:9

Name:box1

a:8

b:10

before erasing

after erasing

Name:box2

a:0

b:9

Name:box3 box5

a:1

b:3

Name:box5

a:4

b:10

Name:box4

a:5

b:9

Name:box1

a:8

b:10

Program ended with exit code: 0

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
In the following C code, Which variable if NOT of primitive data type? A. a B....
In the following C code, Which variable if NOT of primitive data type? A. a B. b C. c D. d int a = 10; double b = 20.0; float c = false; char d[5] = "Hello"; // here we define a string In programming language C, the implementation of a string data type is limited dynamic length, which means the length of a string variable is fixed once it has been defined. A. True B. False In C# programming...
Question 1 Which statement is false about what Data Types defines Question 1 options: What values...
Question 1 Which statement is false about what Data Types defines Question 1 options: What values a variable cannot hold? How much memory will be reserved for the variable? What value a variable will hold? How the program will use the data type? Question 2 Using the structure below, which of the following statements about creating an array (size 20) of structures are not true? struct Employee{     string emp_id;     string emp_name;     string emp_sex; }; Question 2 options:...
Write a program that accepts as input the mass, in grams, and density, in grams per...
Write a program that accepts as input the mass, in grams, and density, in grams per cubic centimeters, and outputs the volume of the object using the formula: volume = mass / density. Format your output to two decimal places. ** Add Comments ** Print Name and Assignment on screen ** Date ** Submit .cpp file. Demo // This program uses a type cast to avoid an integer division. #include <iostream> // input - output stream #include <fstream> //working file...
C++ Question I have a text file that contains data from a CD (ex 1. Adagio...
C++ Question I have a text file that contains data from a CD (ex 1. Adagio “MoonLight” Sonata - Ludwig Van Beethoven /n 2. An Alexis - F.H. Hummel and J.N. Hummel) How do I sort the data by author since that information is in the middle of the string? Here's what I have that sorts the string from the beginning: if (file.is_open())    {        while (getline(file, line))        {            lines.push_back(line);        }...
c++ program can you please explain how it works and the process? Question: You will design...
c++ program can you please explain how it works and the process? Question: You will design a program in C++ that plays hangman using classes (polymorphism and inheritance).... Hangman Game CODE: #include <iostream> #include <cstdlib> #include<ctime> #include <string> using namespace std; int NUM_TRY=8; //A classic Hangman game has 8 tries. You can change if you want. int checkGuess (char, string, string&); //function to check the guessed letter void main_menu(); string message = "Play!"; //it will always display int main(int argc,...
I need to change an integer and put it into an array but in a way...
I need to change an integer and put it into an array but in a way that it is read correctly. Arrays are typically drawn to be read left to right with the 0 element on the left and the largest on the right. However, arrays are a completely made up concept and are not physical in nature. So you can draw them and think about them anyway you want. For this problem having the right side as the 0...
."Ask the user to input a number.  You must use an input dialog box for this input....
."Ask the user to input a number.  You must use an input dialog box for this input. Be sure to convert the String from the dialog box into an integer (int). The program needs to keep track of the smallest number the user entered as well as the largest number entered. Use a Confirm dialog box to ask the user if they want to enter another number. If yes, repeat the process. If no, output the smallest and largest number that...
Strings The example program below, with a few notes following, shows how strings work in C++....
Strings The example program below, with a few notes following, shows how strings work in C++. Example 1: #include <iostream> using namespace std; int main() { string s="eggplant"; string t="okra"; cout<<s[2]<<endl; cout<< s.length()<<endl; ​//prints 8 cout<<s.substr(1,4)<<endl; ​//prints ggpl...kind of like a slice, but the second num is the length of the piece cout<<s+t<<endl; //concatenates: prints eggplantokra cout<<s+"a"<<endl; cout<<s.append("a")<<endl; ​//prints eggplanta: see Note 1 below //cout<<s.append(t[1])<<endl; ​//an error; see Note 1 cout<<s.append(t.substr(1,1))<<endl; ​//prints eggplantak; see Note 1 cout<<s.find("gg")<<endl; if (s.find("gg")!=-1) cout<<"found...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world...
Write a 4-6 sentence summary explaining how you can use STL templates to create real world applications. In your summary, provide an example of a software project that you can create using STL templates and provide a brief explanation of the STL templates you will use to create this project. After that you will implement the software project you described . Your application must be a unique project and must incorporate the use of an STL container and/or iterator and...
Hi there, I've been asked to write a program in C which can read values from...
Hi there, I've been asked to write a program in C which can read values from a file then sort them, and then write to a binary file. I'm getting stuck when I write my binary file as the output is just spitting out garbage values and not the values that are being read in. When I print my input file reader everything is perfect but after sorting and then writing, the output is completely wrong. I have checked that...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT