检查字符串是否使用STD :: MAP包含重复
Check if a string contains duplicates using std::map
i具有一个函数,该函数是否通过将每个char作为钥匙放置,使用std ::映射包含重复。无法弄清楚为什么这不起作用。
#include<iostream>
#include<map>
#include<string>
int unique_char(std::string s){
for(int i=0 ; i < s.size(); i++ )
{
std::map<char,int> uniq_hash_table;
std::pair<std::map<char,int>::iterator,bool> ret;
ret = uniq_hash_table.insert(std::pair<char,int>(s[i],0));
if(ret.second==false)
{
std::cout << "The string contains duplicates" << std::endl;
return 1;
}
}
return 0;
}
int main()
{
std::string s="abcd";
std::string s1="aabc";
if(unique_char(s)==0){
std::cout << "The 1st string does not contain duplicates" << std::endl;}
if(unique_char(s1)==0){
std::cout << "The 2nd string does not contain duplicates" << std::endl;}
return 0;
}
对于两个示例,程序返回"字符串不包含重复项"。
ps:我故意使用std :: map来获取o(n)解决方案。
您的解决方案不起作用,因为您的std::map<char,int>
在循环的每次迭代中都重新创建。然后,在循环的每次迭代中,地图都是空的。然后,没有重复。
最好使用std::set<char>
。您可以做这样的事情:
bool contains_duplicated_char(const std::string& s)
{
std::set<char> check_uniq;
for(unsigned long int i = 0; i < s.length(); ++i)
if(!check_uniq.insert(s[i]).second)
return true; // Duplicated char found
return false; // No duplicated char found
}
然后以这种方式调用:
const std::string str = "abcdefghijklamnopb";
const bool dupl = contains_duplicated(str);
为了使您的代码更加通用(管理更多数据类型),您还可以以这种方式创建您的功能:
template <typename Type, typename IteratorType>
bool contains_duplicated(IteratorType begin, IteratorType end)
{
std::set<Type> check_uniq;
for(IteratorType it = begin; it != end; ++it)
if(!check_uniq.insert(*it).second)
return true;
return false;
}
然后将其称为:
std::vector<std::string> vec_str;
vec_str.push_back("Foo");
vec_str.push_back("Bar");
vec_str.push_back("Baz");
vec_str.push_back("Bar");
const bool dupl = contains_duplaicated<std::string>(vec_str.begin(), vec_str.end());
//...
const std::string str = "abcdefab";
const bool dupl2 = contains_duplacated<char>(str.begin(), str.end());
//...
const std::deque<long int> x(4, 0);
x[0] = 1;
x[1] = 17;
x[2] = 31;
x[3] = 0;
const bool dupl3 = contains_duplicated<long int>(x.begin(), x.end());
它不起作用,因为uniq_hash_table
是为for
循环中的每个符号重新创建的。
在for
循环之前,尝试将其移至函数的开头:
std::map<char,int> uniq_hash_table;
for(int i=0 ; i < s.size(); i++ )
{
// ...
}
作为您的地图定义在for循环的正文中,您在每次迭代时重新创建一个空的地图。
在循环外声明您的容器,并且''会更好。
请注意,如果您从不增加int值,则可以使用集合而不是地图。
相关文章:
- 如何导出包含具有"std::unique_ptr"值的"std::map"属性的
- 仅包含可移动 std::map 的类的移动构造函数不起作用
- 遍历 std::set 中包含的所有三重不同值?
- 如何为包含map<int,*double>成员的类编写析构函数?
- 使用std :: map时,包含unique_ptr的结构中的默认破坏者会导致编译错误
- 是包含线程局部变量重入的函数
- 指向结构的指针保留某些属性,同时将其他属性重置为零(使用 map)
- 包含指向自身的智能指针的对象,该指针在对象超出范围之前未重置
- 使用 stl::map 和 stl::unordered_map 对包含大量重复元素的数组数据进行排序
- 检查字符串是否使用STD :: MAP包含重复
- 如何实现 std::map::查找包含两个结构'Pos'结构的比较逻辑,每个结构包含 x 和 y 坐标
- 正在重试包含同一异常的exception_ptr的多个实例
- 检查map<string,string>是否包含另一个map<string,string>
- 包含<map>后编译错误
- 重新排序包含目录如何解决宏重定义问题
- 将包含类的类复制到 std::map
- 将函数应用于std::map值,其中包含for each和lambda函数
- 如何重定向头文件(到库)以在实例化中包含一些代码
- 为什么在使用g++ 4.8.4时,这段代码会产生一个包含单个元素的map ?
- for-each 循环生成错误,但 for(it=begin()...在遍历 std::map 包含 unique_ptr 时不会