algorithm count doesn't add last cell

Hi there :D
I'm just trying my luck on some challenges I found on the internet, but I'm starting to have problems with the count algorithm. I pretty much have my program set up to solve the challenge but the count algorithm does weird things:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
using namespace std;
#include <string>
#include <algorithm>

int main(){
	bool iselimin=true;
	string one;
	string two;
	cin >> one;
	cin >> two;
	int dieter;
	one.append(two);
	for(int a=0; a<one.size(); a++){
		dieter=count(&one[0], &one[one.size()-1], one[a])%2;
		if(dieter==0){
			
		}
		else if(dieter!=0){
			iselimin=false;
		}
		cout << count(&one[0], &one[one.size()-1], one[a]) << endl;
	}
	if(iselimin==true){
		cout << "Yes!";
	}
	else if(iselimin==false){
		cout << "No!";
	}
	return 1;
}


line 15 uses the count function:

count(&one[0], &one[one.size()-1], one[a])

and the cout in line 22 (which was for test purposes) then printed out how many of each letter there where.

but oddly enough, when i have the following input "abc cba"
i get the output "1, 2, 2, 2, 2, 1" meaning that there only is one 'a' in the string???

the same happens with "bac cba" printing the following: "2, 1, 2, 2, 2, 1" meaning, that again there only is one 'a'...

this lead me to think that the count function in my case doesn't seem to acknowledge the last cell of the string for some reason.

I don't really know what to do in this situation. Is there any way to fix this?
greetings
Last edited on
Hi

cin only retrieves info up to the first whitespace. you need a getline function. Google it.


Hope this was a good clue.

I actually have two times cin, the first time I enter "abc" e.g. into the string, the second time "cba" and in line 13 I then append them, so my string is "abccba"
The algorithms in the STL expect iterators in range [)
That's why end() is one pass the last element.
Use the iterators cout << count(one.begin(), one.end(), one[a]) << endl;
http://www.cplusplus.com/reference/algorithm/count/
Returns the number of elements in the range [first,last) that compare equal to value.


So the "last" is not counted. Try changing your code to count(&one[0], &one[one.size()], one[a])
@naraku9333, ne555
thanks, this version worked ;)

@fg109
i actually tried this before creating this thread and it went out of the range of the string o.0
Ok, i was reading your description, & not the code my bad.


The count function does this:

Returns the number of elements in the range [first,last) that compare equal to value.


so why the for loop to cycle through the array?




or also: count(one.begin(), one.end(), one.at(a))

Don't want to tell you how to write your code, but find() might be a more appropriate function than count() in your example.
Last edited on
We were writing our replies at the same time, fg109 happened to quote the same thing from the documentation!
@LowestOne
I don't quite get how it would work with find? I mean I basically have to count whether a letter exists (within the string) an even number of times or an odd number of times.
an even number of times or an odd number of times.


Ah, okay. Carry on with count then :)
Topic archived. No new replies allowed.