C/C++ Why aren't insert, erase, and find working in my script?

  • Thread starter Thread starter member 428835
  • Start date Start date
AI Thread Summary
The discussion centers around troubleshooting a C++ script designed to count the largest number of non-repeating strings in a vector. The main issues highlighted include the incorrect use of the `insert`, `erase`, and `find` methods on an `unordered_set`. It is noted that the script attempts to insert characters instead of strings, leading to type mismatch errors. The importance of using the correct data type for the `unordered_set` is emphasized, as it should contain strings rather than characters. Additionally, there are suggestions to simplify the approach and consider using Python for its ease of handling strings and lists. The conversation also touches on the complexities of C++ syntax and its performance advantages. Ultimately, the focus remains on correcting the data type issues and understanding the underlying logic of the script.
member 428835
Hi all, I'm trying to write a small script to count the largest number of non-repeating strings in a vector. Below is what I wrote, but insert, erase, and find aren't working. Any ideas why?

If the script isn't correct, you don't have to correct it. I'm just wondering why those three commands don't work.

C++:
#include <iostream>
#include <unordered_set>
int long_sub(std::string s) {
    int count = 0, L = 0;
    std::unordered_set<std::string> used_str;
    int max_count = 0;
    for(int R = 0; R < s.length(); R++) {
        if(used_str.find(s[R]) != used_str.end()) {    // IF s[R] IS IN used_str  
            used_str.erase(s[L]);
            L += 1;
            count -= 1;
        } else { // IF s[R] IS NOT IN used_str
            used_str.insert(s[R]);
            R += 1;
            count += 1;
        }
        max_count = std::max(max_count, count);
    return count;
    }
}
int main()
{
    std::string s = "abcabcbb";
    auto sol = long_sub(s);
    std::cout << sol;
}
 
Technology news on Phys.org
joshmccraney said:
If the script isn't correct, you don't have to correct it. I'm just wondering why those three commands don't work.
Mainly because you have not set any value to used_str, but also erase is not working because the R'th character of a string is not an iterator pointing to an element of an unordered_set.

Anyway how do you know these "commands aren't working"? Is there an error message?
 
pbuk said:
Mainly because you have not set any value to used_str, but also erase is not working because the R'th character of a string is not an iterator pointing to an element of an unordered_set.

Anyway how do you know these "commands aren't working"? Is there an error message?
Can you elaborate on setting a value to used_str? I tried changing the code to something like this for what you said regarding the iterator, but perhaps I'm misunderstanding:
C++:
#include <iostream>
#include <unordered_set>
#include <algorithm>
#include <typeinfo>
int long_sub(std::string s) {
    int count = 0, L = 0;
    std::unordered_set<std::string> used_str;
    int max_count = 0;
    auto L = s.begin();
    for(auto R = s.begin(); R != s.end(); R++) {
        if(used_str.find(*R) != used_str.end()) {    // IF s[R] IS IN used_str  
            used_str.erase(*L);
            L++;
            count -= 1;
        } else { // IF s[R] IS NOT IN used_str
            used_str.insert(*R);
            R++;
            count++;
        }
        max_count = std::max(max_count, count);
    return count;
    }
}
int main()
{
    std::string s = {"abcabcbb"};
    auto sol = long_sub(s);
    std::cout << sol;
}
 
Last edited by a moderator:
joshmccraney said:
Can you elaborate on setting a value to used_str?
C++:
    // Instead of...
    std::unordered_set<std::string> used_str;
    // ...you need:
    std::unordered_set<std::string> used_str = {};
 
pbuk said:
C++:
    // Instead of...
    std::unordered_set<std::string> used_str;
    // ...you need:
    std::unordered_set<std::string> used_str = {};
I tried that before too, but still getting the error "no matching member function for call to 'insert'". Any idea? I've googled it but can't find the error
 
joshmccraney said:
I tried that before too, but still getting the error "no matching member function for call to 'insert'". Any idea? I've googled it but can't find the error
insert expects an argument of type std::string, *R is a char.
Don't you want to find substrings of your input string?
 
  • Like
Likes pbuk
joshmccraney said:
"no matching member function for call to 'insert'"
means that you are passing an argument of the wrong type to 'insert'. If you create your unordered set with std::unordered_set<std::string> used_str = {} then you need to pass a <std::string> to insert, not an iterator over a <std::string> as you are trying to do in #5 or a single character as you are trying to do in #1.

I think you are trying to run in CPP before you can walk: you need to go back to the basics before you attempt something like this.

And why are you doing this in CPP anyway, Python is a much easier language to work with structures like strings and lists?
 
pbuk said:
C++:
    // Instead of...
    std::unordered_set<std::string> used_str;
    // ...you need:
    std::unordered_set<std::string> used_str = {};
These lines have the same effect. There is no need to "assign a value" to used_str. It is default-constructed.
 
  • Informative
Likes pbuk
Passerby said:
These lines have the same effect. There is no need to "assign a value" to used_str. It is default-constructed.
Oh how I hate C++ and its "if there are ten different ways of doing something implement them all" design outcome.
 
  • #10
pbuk said:
Oh how I hate C++ and its "if there are ten different ways of doing something implement them all" design outcome.
Yea but it's fast. Real fast.
 
  • #11
Also, got it, thanks!
C++:
#include <iostream>
#include <unordered_map>
#include <string>
class Solution {
public:
    int lenLonStr(std::string s) 
    {
        int max_len = 0;
        int L = 0;
        int R = 0;
        int n = s.length();
        std::unordered_map<char, int> char_map;
        while (R < n)
        {
            char c = s[R];
            std::unordered_map<char, int>::iterator it = char_map.find(c);
            if (it == char_map.end())
            {
                char_map[c] = R;
                R++;
                if (char_map.size() > max_len)
                    max_len = char_map.size();
            }
            else
            {
                L = it->second + 1;
                char_map.clear();
                R = L;
            }
        }
        return max_len;
    }
};
int main()
{
    std::string s = "abcshabcbb";
    Solution ob1;
    auto sol = ob1.lenLonStr(s);
    std::cout << sol <<"\n";
    Solution* ptr = &ob1;
    int a = ptr->lenLonStr(s);
    std::cout << a <<"\n";
    std::shared_ptr<Solution> ptr2 = std::make_shared<Solution>();
    int b = ptr2->lenLonStr(s);
    std::cout << b;
}
 

Similar threads

Back
Top