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

  • Context: C/C++ 
  • Thread starter Thread starter member 428835
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around a script intended to count the largest number of non-repeating strings in a vector, focusing on issues related to the use of the insert, erase, and find commands in C++. Participants express confusion over why these commands are not functioning as expected in the provided code.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant notes that the commands may not work because the unordered_set is not being populated correctly and that the erase command fails since the R'th character of a string is not an iterator pointing to an element of an unordered_set.
  • Another participant suggests that the unordered_set should be initialized with a value, but others argue that it is default-constructed and does not require explicit initialization.
  • There is a discussion about the type of argument expected by the insert function, with one participant pointing out that a character is being passed instead of a string, which leads to an error.
  • Some participants express frustration with C++'s complexity and suggest that Python might be a simpler alternative for handling strings and lists.
  • A later reply introduces a different approach using an unordered_map to track characters and their indices, indicating an alternative method to solve the problem.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the correct approach to resolve the issues with the commands. Multiple competing views on initialization and type handling persist throughout the discussion.

Contextual Notes

There are unresolved questions regarding the correct handling of types in C++, particularly concerning the use of characters versus strings in the context of unordered_set operations. Additionally, the discussion reflects varying levels of familiarity with C++ among participants.

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   Reactions: 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   Reactions: 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

  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
Replies
73
Views
6K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
1
Views
3K