Here's the problem I wanted to make a program to output how many times there is a repeating string and at which positions of a list(it's the printer function).
and it worked.
but here's the problem my vector thinks it has 1 string when it should have all the repeating strings.
and no matter what method i try to get the strings I end up with 1 unless I hard code it to the number of strings that should be in there and it works as intended.
Why does it happen?
I haven't used the printer function because I wanted to find out where the problem is, but couldn't and that's why I'm asking here.
string apple repeats 2 at 1
3
string orange repeats 1 at 4
string kiwi repeats 0 at
Looking at the rest of your code, line 73 is probably wrong because it will always be tree when i==j and i!=0. If you're trying to remove duplicates then change line 73 to if (i!= j && name == vectorofnames[j]) Then you don't need b at all.
void printer(const std::string& stringer,
const std::list<std::string>& listofnames)
{
std::vector<std::size_t > kyle; // I prefer size_t here rather than int
std::size_t repeat = 0;
std::size_t position = 0;
for(auto&& item : listofnames) // uses forward reference, the best practise.
{
if(stringer == item)
{
repeat++;
kyle.push_back(position+1);
}
position++;
}
std::cout << "string " <<stringer << " repeats " << repeat << " at ";
// repeat is equal to size of kyle, so can use range based for as well
for(auto&& val : kyle)
{
std::cout << val << "\n";
}
}
I also put some spaces in the code to relieve the density.
I am not so sure about setting bool values to NULL, why not false?
It is also better to declare 1 variable per LOC.
Does the container have to be a list? I don't see anything to preclude the use of a std::vector.
Pedantically, if using a do loop, always put the while part on the same line as the closing brace.
There are other ways of doing this, but not as safe:
for (constauto& item : container) // const lvalue reference with auto type deduction
for (constint item : container) // const with explicit type, pass by value
Note that one can't have :
[code]for (const auto&& item : container)
To have const with a forward reference, it seems to me that one must put the for loop in a function, and have the container variable as a const parameter.
std::size_t is usually the largest unsigned type the system has (typically unsigned 64 bit), and is the return type for size() functions in STL containers. One should use this type to prevent warnings about type mismatch, and avoid implicit casts from std::size_t to int say. I tend to use it anywhere an unsigned type is appropriate, I don't often have to worry about the 64 bit size versus a 32 bit int.