加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – binary_search不适用于矢量

发布时间:2020-12-16 06:48:06 所属栏目:百科 来源:网络整理
导读:#include iostream#include string#include vector#include algorithmusing namespace std;int main(void){ string temp; vectorstring encrypt,decrypt; int i,n,co=0; cin n; for(i=0;in;i++) { cin temp; encrypt.push_back(temp); } for(i=0;in;i++) { c
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
int main(void)
{
    string temp;
    vector<string> encrypt,decrypt;
    int i,n,co=0;
    cin >> n;
    for(i=0;i<n;i++)
    {
        cin >> temp;
            encrypt.push_back(temp);
    }
    for(i=0;i<n;i++)
    {
        cin >> temp;
        decrypt.push_back(temp);
    }
    for(i=0;i<n;i++)
    {
        temp = encrypt[i];
        if((binary_search(decrypt.begin(),decrypt.end(),temp)) == true) ++co;
    }
    cout << co << endl;
    return 0;
}

它读取两个相等的字符串列表,并且应该打印出第一个列表中有多少单词也可以在第二个列表中找到,简单.
不给我经过考验的结果,我认为问题出在binary_search中.
你能告诉我为什么吗 ?

解决方法

因为字符串没有在向量中排序.首先使用 std::sort对它们进行排序.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读