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

  • Thread starter Thread starter member 428835
  • Start date Start date
Click For Summary
SUMMARY

The forum discussion focuses on issues with the C++ script for counting the largest number of non-repeating strings in a vector. The primary problems identified are related to the incorrect use of the std::unordered_set for character insertion and erasure, specifically that characters from the string are being treated as std::string instead of char. The solution involves using std::unordered_map for character tracking and ensuring that the correct data types are used for insertion and erasure operations.

PREREQUISITES
  • Understanding of C++ data structures, specifically std::unordered_set and std::unordered_map
  • Familiarity with string manipulation in C++
  • Knowledge of iterators and their usage in C++
  • Basic understanding of algorithmic complexity related to string processing
NEXT STEPS
  • Learn about C++ iterators and their proper usage with std::unordered_set
  • Study the differences between std::unordered_set and std::unordered_map in C++
  • Explore algorithms for finding the longest substring without repeating characters
  • Practice debugging C++ code to identify type-related errors in data structures
USEFUL FOR

C++ developers, software engineers working on string manipulation algorithms, and anyone interested in optimizing character processing in C++ applications.

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

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