如何以正确的方式找到向量中的元素-C
how to find an element in vector in the right way - C++
我正在尝试从向量删除元素。但是我认为,我有一个特定的问题:
我的数据来自阅读文件:
移动ctrl a,f3
复制ctrl v,shift v
搜索F3,F4
- 尝试从文件中读取
- 输入一个单词,我想听到它(e.x。:移动)
-
的问题是,我只需要输入一个(例如示例中的)单词,然后将整个命令字符串( move ctrl a,f3 )。
我需要的只是一个单词找到字符串。但是在下面的代码中,我无法执行此操作,请帮助,请解决问题。在下面的代码中,我只能找到一个单词,如果在文件中只有一个单词,而不是( move ctrl a,f3 ),但是如果字符串由几个单词组成..找不到。
std::vector<std::string> HotMap::remove_element(std::vector<std::string> MyVector){ //remove a element ,reading a vector of cmds, then deletting one chosen of them std::string delete_str; std::vector<std::string>::iterator iter_vec; std::cout << "Enter an element,which you want to delete: " << std::endl; std::cin >> delete_str; for (auto & c : delete_str) c = tolower(c); //For the standard for the right searching,all letters are in lower registr //Here, iter_ve is my vector(vec_get_smd) it swiches to algorithm //std::ind that look for a cmd command ,which user is trying to input iter_vec = std::find(MyVector.begin(), MyVector.end(), delete_str); if(iter_vec!= MyVector.end()) //Check if is there an element that yoy want earse { std::cout << "Found a element and earsed it!" << std::endl; MyVector.erase(iter_vec); } else { std::cout << "The element wasn't found!" << std::endl; } for (iter_vec = MyVector.begin(); iter_vec != MyVector.end(); ++iter_vec) //Just for myself, showing results { std::cout << *iter_vec << std::endl; } return std::vector<std::string>(MyVector); }
我的结果:
read KEYS from a file TWO,below
move,fsf,fsdf,aaa
copy
search
Enter an element,which you want to delete:
move
The element wasn't found!
我想,您需要尝试使用std::search()
:
std::vector<std::string>
delete_word(" move");
auto iterator=std::search (MyVector.begin(),MyVector.end(),delete_word.begin(), delete_word.end());
它试图在MyVector
中找到您的 delete_word 。
相关文章:
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- 在 C++ 中输出枚举类类型的向量元素
- 用向量的向量元素初始化向量的空向量
- 为什么不允许使用可变长度数组作为向量元素?
- 在函数中传递向量元素
- 使用 Rcpp 加速替换迭代算法中的列表和向量元素是否合法?
- 返回向量元素的 l 值的正确方法是什么?
- 当向量增长时,指向向量元素的C++指针是否无效
- 如何在C++中从一个向量元素减去std::unorderede_map向量元素并更新它
- C++:向量元素赋值导致访问冲突
- 将向量元素与字符串元素进行比较,而不初始化向量
- 从2D矢量中找出最小尺寸的向量元素的更好方法
- 当我尝试将一个向量元素的值分配给另一个向量元素时,为什么我的应用程序会崩溃
- 使用递归C++向量元素的总和
- 如何查找为数组中的最低值编制索引的向量元素
- 限制指针访问STL ::向量元素
- 访问索引指向向量元素时的seg故障 - 做错了什么
- 如何使用指针访问向量元素的各个元素
- 向量元素数据损坏了Find()操作
- 将字符值分配给向量元素.C