Reputation: 263
Thank you for taking your time to read this! I have been practicing online and I came across this one problem which I am not able to solve.
Firstly, we enter a number and then enter some names. The number is the size of a vector<string>
where we put the names. Afterwards, the elements of the said vector<string>
are to be copied to a list<string>
.
Afterwards, the program is supposed to copy the names to a vector<set<string>>
where each set represents a team. The size of the teams is to be determined by a formula and I have no problems in calculating the sizes(for example, if the number of children is 10, and you wish to place them in three teams, then the sizes of the teams should be 4, 3, 3 respectively.)
Now here comes the tricky part for me. The copying is to be done as follows:
The first entered name is copied to the first set<string>
. Afterwards, you take the number of letters in the name and iterate through the list<string>
number of letters times. (If at one point you reach the end of the list, the iterator goes back to the beginning of the list)
The name you stop at is to be put into the current set until it is full. When you put the name in the corresponding set, it is then to be removed from the list.
I do not know how to do this. I get stuck right after I put the first name in the first set<string>
and remove it from the list<string>
I do not know how to return the iterator back at the beginning of the list if it reaches the end of the list and I do not know how to skip to the next set<string>
if the current one is full.
Here is what I tried:
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <list>
using namespace std;
int LettersInWord(string s) { // a "letter" is an alphabetic character or a number
int counter = 0;
for(int i = 0; i < s.length(); i++) {
if((s[i] >= 65 && s[i] <= 90) || (s[i] >= 97 && s[i] <= 122) ||
(s[i] >= 48 && s[i] <= 57))
counter++;
}
return counter;
}
int main() {
vector<set<string>> vss(3); // three teams
list<string> names = {{"Name1"}, {"Name2"}, {"Name3"}, {"Name4"}, {"Name5"},
{"Name6"}, {"Name7"}, {"Name8"}, {"Name9"}, {"Name10"}};
vector<int> size_teams = {4, 3, 3};
auto it = names.begin();
string s = *it;
vss[0].insert(*it); // put the first name in the first set
it = names.erase(it); // erase it
int number_of_iterations = LettersInWord(s);
int counter_of_insertions = 0; // this counter keeps track of
// how many strings are inserted in a set, so
// as not to overflow the size
for(int i = 0; i < vss.size(); i++) { // iterate through the vector of sets
int counter_of_iterations = 0; // we compare this to counter_of_insertions
for(auto it = names.begin(); it != names.end(); it++) { // iterate through the list
if(it == names.end())
it = names.begin(); // if iterator is at the end of list, return it to
// beginning
counter_of_iterations = 0;
if(counter_of_iterations == number_of_iterations) {
vss[i].insert(*it); // insert word
counter_of_insertions++;
if(counter_of_insertions == size_teams[i]) i++;
counter_of_insertions = 0;
it = names.erase(it);
number_of_iterations = LettersInWord(*it); // get no of letters in next word
}
}
}
return 0;
}
What this does is it just copies the first name to the first set and does absolutely nothing else.
Whatever I try I simply cannot fix this. Would anyone be kind to make a modification to the code above? I am sorry for any bad phrasing, mistakes or errors.
Note: It is mandatory to use list<string>
and <vector<set<string>>
Thanks to anyone who is willing to help in any way!
Upvotes: 1
Views: 124
Reputation: 19761
if(counter_of_iterations == number_of_iterations) {
This line can never be true.
It's easy to see in godbolt, because there's a huge chunk of code that doesn't get colored (meaning that it didn't have any machine code generated for it):
Also, line 39 can never run either, since that's the termination case for the for loop it's in.
Upvotes: 2