map.find() 找不到键 C++
map.find() not finding key c++
我正在以路径模式(db/collection/_id)从MongoDB获取值,并尝试将数据插入到映射中(Key=_id,value=完整路径)。但是,当我尝试通过将键值传递给函数 map.find() 来查找某些映射数据时,即使键存在,它也会返回 map.end()。
下面是我实现的代码,请查看此内容并让我知道我出错的地方。
class myStructure {
std::vector<string> arr;
public:
std::map<std::string, std::string> myMap;
public:
void add(char *ptr, size_t len, FILE *stream) {
std::string key;
unsigned found;
while (getline(&ptr, &len, stream) != -1) {
std::string path(ptr);
found = path.find_last_of("/\");
key = path.substr(found + 1);
myMap[key] = path;
}
// Iterating entire Map
std::map<string, string>::iterator its1 = myMap.begin();
std::cout << "myMap contains:n";;
for (its1 = myMap.begin(); its1 != myMap.end(); ++its1)
std::cout << its1->first << " => " << its1->second << 'n';
// Output
//59dd9db3b3defb36a894a0f1 => /test/restaurants/59dd9db3b3defb36a894a0f1
std::string key_to_erase;
std::cout << "Enter Key value to remove:" << std::endl;
std::cin >> key_to_erase;
//Erasing provided key value
std::map<std::string, std::string>::iterator iit = myMap.find(key_to_erase);
if (iit == myMap.end()) {
std::cout << "key not foundn";
}
// Check if iterator is valid.
else {
// Remove the element pointed by iterator
myMap.erase(key_to_erase);
std::cout << "Element Removed" << std::endl;
}
}
};
int main(int, char**)
{
FILE *fpipe;
std::string file;
char *command = "/usr/bin/mongo --eval "db.getSiblingDB(\"admin\").runCommand({ \"listDatabases\": 1 }).databases.forEach(function(database) {db = db.getSiblingDB(database.name);cols = db.getCollectionNames();cols.forEach(function(collectionName) {collection=db.getCollection(collectionName);keys=collection.find();keys.forEach(function(key){print(\"/\"+db+\"/\"+collectionName+\"/\"+key._id);});});});"";
//input values
/*test/restaurants/59dd9db4b3defb36a894cd31
test/restaurants/59dd9db4b3defb36a894cd32
test/restaurants/59dd9db4b3defb36a894cd33
test/restaurants/59dd9db4b3defb36a894cd34*/
char *ptr = NULL;
size_t len;
std::array<char, 128> buffer;
FILE *stream = popen (command, "r");
myStructure ds;
ds.add(ptr, len, stream);
}
如前所述,我们从getline(&ptr, &len, stream)
的键中获得换行符,删除该代码后,该代码适用于用户条目。我使用以下代码来更正以删除换行符
key.erase(std::remove(key.begin(), key.end(), 'n'), key.end());
相关文章:
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 找不到QtResource文件中的文件
- VC++本机单元测试,找不到调试符号
- RegGetValue在当前用户下找不到名称
- 带有 -stdlib=libc++ 的 clang++ 9.0.1 找不到<optional>
- c++找不到具有相同哈希的无序集合元素
- 找不到以下加速库:boost_fiber
- 找不到 FLTK(缺少:FLTK_INCLUDE_DIR)
- 设置 Visual Studio for MPI: 找不到标识符错误
- "assimp/config.h"找不到,但它在文件夹中
- 链接器找不到在虚拟类 c++ 中访问的静态字段的符号
- C++系统找不到指定的文件错误
- FindPackageHandleStandardArgs.cmake:137 的 CMake 错误(消息):找不到 Boost (缺少:正则表达式)(找到合适的版本"1.72.0",
- vcpkg 添加新库,但找不到
- Qt 5.11.2 (Clang 8.0 (Apple), 64 位), 找不到 QJSEngine 文件
- 找不到Linux Visual Studio代码C++文件