Need help for counting unique lines

Hello. I am currently trying to write code for finding unique lines as well as unique words. I have my attempted code below for unique lines but it is not calculating correctly when it comes to unique lines. I believe it is ignoring enters and still counting lines that contain letters, words, sentences, etc. So I need help in figuring out what to add that it will count enters as a line and not count any extra lines that are the same.

Example:
this is a test code
this test code has multiple lines
there are blank lines as well


this line is not the same
this line is not the same

Total unique lines: 5
My code is printing out 5 unique lines but it is not correctly counting the right lines. I know this too because I have tested other lines. As for the unique words, I don't even know how to get started :/


1
2
3
4
5
6
7
8
9
unsigned long countULines(const string& s)
{
  set<string> wl;
  string ulines;
  for(int ul = 0; ul < s.size(); ul++){
      wl.insert(ulines);
  }
  return wl.size();
}
1
2
3
4
5
unsigned long getUniqueLines(const std::string& s) {
	static std::set<std::string> container;
	container.insert(s);
	return container.size();
}


This should work, if I understand what it is you're trying to achieve.
You pass a line to the function as a string. The first time the function gets called, it instanciates a static std::set<std::string>, which means the container's life time is not constricted by the scope of the function.
Then, you insert the line to the set. If the same line already exists within the container, the size of the container will remain the same.
I found the solution to counting unique lines and now I need to figure out how to count unique words. Thanks though!
Last edited on
Something like this:
1
2
3
4
5
6
7
8
9
10
11
12
unsigned long getUniqueWords(const std::string s) {
	static std::set<std::string> container;
	std::string word = "";
	for(unsigned long i=0; i<s.size(); ++i) {
		if(s[i]==' ' && i!=0) {
			container.insert(word);
		}else {
			word+=s[i];
		}
	}
	return container.size();
}


This does not differentiate between upper- and lowercase characters.
It also doesn't account for punctuation.
Topic archived. No new replies allowed.