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

  • C/C++
  • Thread starter member 428835
  • Start date
In summary, the conversation discusses a script for counting the largest number of non-repeating strings in a vector. The code provided is not working properly, and the conversation delves into the reasons why certain commands, such as insert, erase, and find, are not functioning correctly. Suggestions are made for correcting the code, including setting a value for used_str and using appropriate arguments for the insert command. The conversation also discusses alternative approaches to the problem, such as using an unordered map.
  • #1
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
  • #2
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?
 
  • #3
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:
  • #4
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 = {};
 
  • #5
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
 
  • #6
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
  • #7
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?
 
  • #8
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
  • #9
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;
}
 

1. Why aren't insert, erase, and find working in my script?

There could be several reasons why these functions are not working in your script. One possibility is that you have not properly defined or initialized the data structure you are trying to use these functions on. Another possibility is that you have made a mistake in your code, such as a typo or incorrect syntax. It is also possible that there is a bug in the implementation of these functions. Lastly, the input data you are using may not be compatible with the functions, resulting in unexpected behavior.

2. How can I troubleshoot why insert, erase, and find are not working in my script?

To troubleshoot these functions, you can start by carefully reviewing your code and checking for any errors or inconsistencies. You can also use debugging tools to step through your code and see where the problem may be occurring. Additionally, you can try using different input data or testing the functions on a smaller scale to pinpoint the issue. It may also be helpful to consult documentation or seek assistance from others who have experience with these functions.

3. Is there a specific way to use insert, erase, and find in my script?

Yes, these functions may have specific requirements for their input data or parameters. For example, insert may require the data to be in a certain format or for the index to be within a specific range. Erase may need a specific value or range to be specified for the data to be removed. Find may only work with certain data types or may require a specific condition to be met. It is important to carefully read the documentation for these functions and ensure that you are using them correctly.

4. Could the issue be with the data I am trying to use insert, erase, and find on?

Yes, the issue could be with the data you are trying to use these functions on. For example, if the data is not in the correct format or does not meet the requirements for the function, it may not work as expected. Additionally, if the data is too large or complex, it may cause the functions to take longer to execute or result in unexpected behavior. It is important to ensure that your data is compatible with these functions and to consider the size and complexity of the data.

5. How can I fix the issue with insert, erase, and find not working in my script?

The solution to fixing these functions will depend on the specific issue you are facing. Some potential solutions may include fixing any errors or inconsistencies in your code, ensuring that your data is compatible with the functions, and making sure you are using the functions correctly. It may also be helpful to consult documentation or seek assistance from others who have experience with these functions. If there is a bug in the implementation of the functions, you may need to report it to the developer or find an alternative solution.

Similar threads

  • Programming and Computer Science
Replies
1
Views
871
  • Programming and Computer Science
Replies
1
Views
986
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
3
Views
725
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
5
Views
881
  • Programming and Computer Science
3
Replies
73
Views
4K
  • Programming and Computer Science
Replies
23
Views
1K
Back
Top