简体   繁体   English

map.find()找不到关键的c ++

[英]map.find() not finding key c++

I am fetching values from MongoDB in a path pattern (db/collection/_id) and trying to insert data into a map where (Key=_id, value= full path). 我正在以路径模式(db / collection / _id)从MongoDB中获取值,并尝试将数据插入到映射中(键= _id,值=完整路径)。 However, while I am trying to find certain map data by passing the key value to the function map.find(), it is returning map.end() even though the key exists. 但是,当我尝试通过将键值传递给函数map.find()来查找某些地图数据时,即使键存在,它也会返回map.end()。

Below is the code I have implemented, please look into this and let me know where I am going wrong. 以下是我已实现的代码,请仔细研究一下,让我知道我要去哪里。

        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 found\n";
                }
                    // 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);


        }

As mentioned we are getting newline character in the keys from getline(&ptr, &len, stream) and after removing that code works fine for user entries. 如前所述,我们从getline(&ptr, &len, stream)的键中获取换行符getline(&ptr, &len, stream)并且删除该代码后对于用户输入来说效果很好。 I used below code to correct to remove newline characters 我使用下面的代码来纠正删除换行符

key.erase(std::remove(key.begin(), key.end(), '\n'), key.end());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM