迭代器指向“find

编程入门 行业动态 更新时间:2024-10-28 06:26:55
迭代器指向“find_if”匹配后的第一个元素,为什么?(iterator is pointing to first element after a “find_if” match, why?)

我现在正在教自己c ++,所以我很新。 我正在学习的书中的一个问题是要求比较2个字符串的二元谓词。 下面我复制了我写的内容。 我确信这是一个非常简单的解决方案,但我自己无法弄明白。 基本上,我的错误在if语句中。 当匹配时,它总是打印出第一个元素而不是匹配的元素。 你能帮忙解释一下为什么吗? 我究竟做错了什么? 另外,作为一个新手,如果您看到任何“丑陋的代码”并且可以识别出您的不同内容以便我可以清理它,我会很感激。 谢谢!

#include "stdafx.h" #include <iostream> #include <vector> #include <algorithm> #include <string> using namespace std; //my comparison predicate starts here struct comparison{ string deststring1; string deststring2; comparison (const string& whattheyenter){ deststring1.resize(whattheyenter.size()); transform(whattheyenter.begin(),whattheyenter.end(),deststring1.begin(),tolower); } bool operator () (const string& string2) { deststring2.resize(string2.size()); transform(string2.begin(),string2.end(),deststring2.begin(),tolower); return (deststring1<deststring2); } }; //program begins here int main(){ string comparethisstring; cout<<"enter string to compare: "<<endl; cin>>comparethisstring; vector<string> listofstrings; listofstrings.push_back("my fiRst string"); listofstrings.push_back("mY sEcond striNg"); listofstrings.push_back("My ThIrD StRiNg"); auto ielement = find_if(listofstrings.begin(),listofstrings.end(),comparison(comparethisstring)); if (ielement!=listofstrings.end()){ // when there is a match this always prints "my fiRst string" instead of // pointing to the element where the match is. cout<<"matched:" <<*ielement; } else { cout<<"no match found!"; } return 0; }

编辑:只是想说问题是,首先,我使用了一个小于运算符,这对比较相等是没有用的。 其次,我用cin而不是getline。 结果当我输入“我的第一个字符串”cin时,只分配了“my”来比较这个字符串。 谢谢大家的帮助!

I'm teaching myself c++ right now so I'm pretty new. One of the problem questions in the book I am learning from asks for a binary predicate that compares 2 strings. Below I've copied what I've written. I'm certain this is one of those really simple solutions, but I can't figure it out myself. Basically, my error is in the if statement. When there is a match, it always prints out the first element instead of the element where there was a match. Can you help explain why that is? What am I doing wrong? Also, as a newbie, I'd appreciate it if you see any 'ugly code' and can identify what you would write differently so that I can clean it up. Thanks!

#include "stdafx.h" #include <iostream> #include <vector> #include <algorithm> #include <string> using namespace std; //my comparison predicate starts here struct comparison{ string deststring1; string deststring2; comparison (const string& whattheyenter){ deststring1.resize(whattheyenter.size()); transform(whattheyenter.begin(),whattheyenter.end(),deststring1.begin(),tolower); } bool operator () (const string& string2) { deststring2.resize(string2.size()); transform(string2.begin(),string2.end(),deststring2.begin(),tolower); return (deststring1<deststring2); } }; //program begins here int main(){ string comparethisstring; cout<<"enter string to compare: "<<endl; cin>>comparethisstring; vector<string> listofstrings; listofstrings.push_back("my fiRst string"); listofstrings.push_back("mY sEcond striNg"); listofstrings.push_back("My ThIrD StRiNg"); auto ielement = find_if(listofstrings.begin(),listofstrings.end(),comparison(comparethisstring)); if (ielement!=listofstrings.end()){ // when there is a match this always prints "my fiRst string" instead of // pointing to the element where the match is. cout<<"matched:" <<*ielement; } else { cout<<"no match found!"; } return 0; }

EDIT: Just wanted to say the issue was that first, I used a less than operator, which is no use for comparing equality. Second, I used cin instead of getline. As a result when I typed in "my first string" cin only assigned "my" to comparethisstring. Thanks everyone for the help!

最满意答案

find_if查找predict为true的第一个元素。 你的预测是“它比字母顺序更早,而不是比较comparethisstring 。你可能想要返回true,当且仅当你比较comparethisstring (或deststring1==deststring2 )时。

我还建议让deststring2成为operator()方法的局部变量。

find_if finds the first element for which the predict is true. Your predict is "is it earlier, alphabetically, than comparethisstring. You probably want to return true if and only if you equal comparethisstring (or deststring1==deststring2).

I would also advise making deststring2 a variable local to the operator() method.

更多推荐

本文发布于:2023-08-07 14:42:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464679.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:迭代   find

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!