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
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
Get Answers For Free
Most questions answered within 1 hours.