我是stl的新手.这是我的下面的程序.
typedef pair<string, int> p;
int main(int argc, char *argv[])
{
map<string,int> st;
st.insert(p("hello",1)); //Inserted "hello" as key to map.
st.insert(p("HELLO",1)); //Inserted "HELLO" as key to map.
cout<<"size="<<st.size()<<endl; //Output is 2 because two records found "hello" and "HELLO"
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我不想考虑重复的案例变化(大写字母为小写字母,反之亦然).这里"st.insert(p("HELLO",1));" 应该失败,因此没有.记录应为"1"而不是"2".是否有任何标志设置或类似的?
我无法找到相关问题因此发布了这个问题.
任何帮助都很感激.
P0W*_*P0W 30
使用自定义比较器:
struct comp {
bool operator() (const std::string& lhs, const std::string& rhs) const {
return stricmp(lhs.c_str(), rhs.c_str()) < 0;
}
};
std::map<std::string, int, comp> st;
Run Code Online (Sandbox Code Playgroud)
编辑:
如果您无法使用stricmp或strcasecmp使用:
#include<algorithm>
//...
string tolower(string s) {
std::transform(s.begin(), s.end(), s.begin(), ::tolower );
return s;
}
struct comp {
bool operator() (const std::string& lhs, const std::string& rhs) const {
return tolower(lhs) < tolower(rhs);
}
};
std::map<std::string, int, comp> st;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8905 次 |
| 最近记录: |