C++中比较怎样两个string字符串?

u010179812 2014-08-28 10:55:48
下面的程序是想从一个list中删去大于字符串"a"的字符串,编译通过,运行时有错误,编译器codeblocks.

#include <iostream>
#include <map>
#include <set>
#include <algorithm>
#include <list>
#include <string>
using namespace std;

int main()
{
string one[3] = {"rty", "ab", "bc"};
list<string> two(one, one+3);

list<string>::iterator p;
for(p=two.begin(); p!=two.end(); ++p)
{
if((*p)>"a") //是不是这里比较出错了?C++中字符串不是可以直接比较吗?
{
two.erase(p);
}
}
p = two.begin();
for(int i=0; i<two.size(); i++)
{
cout<<*p<<endl;
p++;
}


return 0;
}


...全文
1463 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
mymtom 2014-08-29
  • 打赏
  • 举报
回复
楼主要注意的是std::remove和list::remove是不同的
FrankHB1989 2014-08-28
  • 打赏
  • 举报
回复
引用 1 楼 AncientLysine 的回复:
string没法和char *比较的, 改成>string("a")
你当没explicit的string构造函数干什么吃的。 出问题的地方4L已经说明白了,迭代器失效。 这类问题的通解是erase-remove idiom:http://en.wikipedia.org/wiki/Erase-remove_idiom。 手撸循环祈祷别手贱。
神奕(deprecated) 2014-08-28
  • 打赏
  • 举报
回复
string有个compare方法
dbzhang800 2014-08-28
  • 打赏
  • 举报
回复
引用 4 楼 starytx 的回复:
运行中断的问题主要出在 列表删除那里,删除后会自动返回下一个迭代器,所以需要做改动: 1:将for中自增部分去掉,循环内作如下修改 if .......... { p = two.erase(p); // 删除后让p指向下一个 } else ++p; // 不删除则迭代器自增
+1 不是字符串比较的问题
  • 打赏
  • 举报
回复
strcmp
starytx 2014-08-28
  • 打赏
  • 举报
回复
运行中断的问题主要出在 列表删除那里,删除后会自动返回下一个迭代器,所以需要做改动: 1:将for中自增部分去掉,循环内作如下修改 if .......... { p = two.erase(p); // 删除后让p指向下一个 } else ++p; // 不删除则迭代器自增
u010179812 2014-08-28
  • 打赏
  • 举报
回复
引用 1 楼 AncientLysine 的回复:
string没法和char *比较的, 改成>string("a")
这样试过了,运行时还是有错。
AncientLysine 2014-08-28
  • 打赏
  • 举报
回复
string没法和char *比较的, 改成>string("a")
u010179812 2014-08-28
  • 打赏
  • 举报
回复
引用 11 楼 mymtom 的回复:

#include <iostream>
#include <map>
#include <set>
#include <algorithm>
#include <list>
#include <string>
using namespace std;
 
bool greater_then_b(const string &s)
{
    return s > string("b");
}

int main()
{
    string one[3] = {"rty", "ab", "bc"};
    list<string> two(one, one+3);
    list<string> three(one, one+3);
 
    list<string>::iterator p;

    for(p=two.begin(); p!=two.end(); )
    {
        if((*p)>"b")    //是不是这里比较出错了?C++中字符串不是可以直接比较吗?
                        // 比较没错,关键在于erase之后 p 成非法的啦, erase返回被删除元素之后的元素
        {
            p = two.erase(p);
        } else {
            ++p;
        }
    }

   p = two.begin();
    for(int i=0; i<two.size(); i++)
    {
        cout<<*p<<endl;
        p++;
    }
 

    // 其实用remove_if是最简单的
    three.remove_if(greater_then_b);
   p = three.begin();
    for(int i=0; i<three.size(); i++)
    {
        cout<<*p<<endl;
        p++;
    }
 
    return 0;
}
今天是学到remove_if时感觉好复杂,才想到不用它也试着能不能达到目的。看来还是要学清楚remove_if.还有往往注意不到函数的返回值,今天又学习到了。
mymtom 2014-08-28
  • 打赏
  • 举报
回复

#include <iostream>
#include <map>
#include <set>
#include <algorithm>
#include <list>
#include <string>
using namespace std;
 
bool greater_then_b(const string &s)
{
    return s > string("b");
}

int main()
{
    string one[3] = {"rty", "ab", "bc"};
    list<string> two(one, one+3);
    list<string> three(one, one+3);
 
    list<string>::iterator p;

    for(p=two.begin(); p!=two.end(); )
    {
        if((*p)>"b")    //是不是这里比较出错了?C++中字符串不是可以直接比较吗?
                        // 比较没错,关键在于erase之后 p 成非法的啦, erase返回被删除元素之后的元素
        {
            p = two.erase(p);
        } else {
            ++p;
        }
    }

   p = two.begin();
    for(int i=0; i<two.size(); i++)
    {
        cout<<*p<<endl;
        p++;
    }
 

    // 其实用remove_if是最简单的
    three.remove_if(greater_then_b);
   p = three.begin();
    for(int i=0; i<three.size(); i++)
    {
        cout<<*p<<endl;
        p++;
    }
 
    return 0;
}
幻夢之葉 2014-08-28
  • 打赏
  • 举报
回复
引用 8 楼 FrankHB1989 的回复:
[quote=引用 1 楼 AncientLysine 的回复:] string没法和char *比较的, 改成>string("a")
你当没explicit的string构造函数干什么吃的。 出问题的地方4L已经说明白了,迭代器失效。 这类问题的通解是erase-remove idiom:http://en.wikipedia.org/wiki/Erase-remove_idiom。 手撸循环祈祷别手贱。 [/quote] 再++
幻夢之葉 2014-08-28
  • 打赏
  • 举报
回复
引用 7 楼 lisong694767315 的回复:
string有个compare方法
++

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧