Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何获取C++;?_C++_Comparison_Vector - Fatal编程技术网

C++ 如何获取C++;?

C++ 如何获取C++;?,c++,comparison,vector,C++,Comparison,Vector,我试图比较两个向量对象,并返回一个包含两个向量中出现的所有字符的向量 如果不编写一些非常复杂的手动方法,将第一个向量中的每个字符与第二个向量中的每个字符进行比较,并使用if将其添加到第三个向量(如果它们匹配,则会返回)中,我该如何处理呢 也许我缺乏向量方面的实际经验,这让我想象这将比实际困难,但我怀疑有一种更简单的方法,我无法通过搜索找到。如果你有字符,也许你应该使用std::strings而不是vectors?字符串有很多搜索等功能。我想您正在寻找。但源向量必须进行排序。如果您不关心输出向量的

我试图比较两个向量对象,并返回一个包含两个向量中出现的所有字符的向量

如果不编写一些非常复杂的手动方法,将第一个向量中的每个字符与第二个向量中的每个字符进行比较,并使用if将其添加到第三个向量(如果它们匹配,则会返回)中,我该如何处理呢


也许我缺乏向量方面的实际经验,这让我想象这将比实际困难,但我怀疑有一种更简单的方法,我无法通过搜索找到。

如果你有字符,也许你应该使用std::strings而不是vectors?字符串有很多搜索等功能。

我想您正在寻找。但源向量必须进行排序。如果您不关心输出向量的顺序,那么可以在源向量的排序副本上运行它

顺便说一句,手动天真的方式并不复杂得可怕。给定两个源向量
s1
s2
,以及一个目标向量
dest
,您可以编写如下内容:

for (std::vector<char>::iterator i = s1.begin(); i != s1.end(); ++i)
{
    if (std::find(s2.begin(), s2.end(), *i) != s2.end())
    {
        dest.push_back(*i);
    }
}
for(std::vector::iterator i=s1.begin();i!=s1.end();++i)
{
if(std::find(s2.begin(),s2.end(),*i)!=s2.end())
{
目的地推回(*i);
}
}
根据您对数据结构的选择,
find
步骤有很多选项。

int temp[5000];//如果你想
int temp[5000]; // declare this globally if you're going to be 
                // doing a lot of set_intersection calls   

int main() {

  char x[]={'a','b','c','d','e'};
  char y[]={'b','c','g'};
  vector<char> v1(x,x+sizeof x/sizeof x[0]);
  vector<char> v2(y,y+sizeof y/sizeof y[0]);
  sort(v1.begin(),v1.end());
  sort(v2.begin(),v2.end());  // the vectors *must* be sorted!!!!!!

  vector<char> inter=vector<char>(temp,set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),temp)); // inter contains {'b','c'}
  int cnt=set_intersection(v1.begin(),v1.end(),v2.begin(),v2.end(),temp) - temp;    // cnt=2

  for(int i = 0; i < (int)inter.size(); ++i) {
    cout<<inter[i]<<" ";
  }
  cout<<endl;

  return 0;
}
//做了很多集合交叉口呼叫 int main(){ 字符x[]={'a','b','c','d','e'}; 字符y[]={'b','c','g'}; 向量v1(x,x+sizeof x/sizeof x[0]); 向量v2(y,y+sizeof y/sizeof y[0]); 排序(v1.begin(),v1.end()); 排序(v2.begin(),v2.end());//向量*必须*排序!!!!!! 向量inter=vector(temp,set_交集(v1.begin(),v1.end(),v2.begin(),v2.end(),temp));//inter包含{b',c'} int cnt=set_交叉点(v1.begin(),v1.end(),v2.begin(),v2.end(),temp)-temp;//cnt=2 对于(int i=0;i<(int)inter.size();++i){ 不能使用。这是一个工作示例:

#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

using namespace std;

int main()
{
    vector<string> v1;
    v1.push_back("Mary");
    v1.push_back("had");
    v1.push_back("a");

    vector<string> v2;
    v2.push_back("a");
    v2.push_back("little");
    v2.push_back("lamb");

    sort(v1.begin(), v1.end());
    sort(v2.begin(), v2.end());

    vector<string> v3;
    set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), back_inserter(v3));

    copy(v3.begin(), v3.end(), ostream_iterator<string>(cout, "\r\n"));
    return 0;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
向量v1;
v1.推回(“玛丽”);
v1.推回(“had”);
v1.推回(“a”);
矢量v2;
v2.推回(“a”);
v2.向后推(“小”);
v2.推回(“羔羊”);
排序(v1.begin(),v1.end());
排序(v2.begin(),v2.end());
向量v3;
设置交叉点(v1.begin(),v1.end(),v2.begin(),v2.end(),back_inserter(v3));
复制(v3.begin(),v3.end(),ostream_迭代器(cout,“\r\n”);
返回0;
}

如果我必须在两个未排序的向量上执行此操作(没有库帮助),我想我应该将其中一个的所有元素添加到哈希表中,然后在第二次查找中迭代每一个元素——应该比先对两个列表进行排序更有效。

这并没有扩展到标准字符类型之外(可能扩展到unicode,具体取决于应用程序),但如果您有兴趣在O(n)时间内完成此操作,则此操作应该有效


#include <vector>
#include <string>
#include <iostream>

std::vector<char> intersect(const std::vector<bool>& x,
                            const std::vector<bool>& y)
{
    std::vector<char> rv;

    std::vector<bool>::const_iterator ix, iy;
    size_t i;

    for (i=0, ix = x.begin(), iy = y.begin();
         ix != x.end() && iy != y.end();
         ++i, ++ix, ++iy)
        if (*ix && *iy) rv.push_back( (char) i);

    return rv;
}

std::vector<bool> poll(const std::vector<char>& x)
{
    std::vector<bool> rv(256, false);

    for (std::vector<char>::const_iterator i = x.begin(); i != x.end(); ++i)
        rv[*i] = true;

    return rv;
}

std::vector<char> build(const std::string& val)
{
    std::vector<char> rv;

    for (size_t i = 0; i < val.size(); ++i)
        rv.push_back(val[i]);

    return rv;
}

int main(int argc, char *argv[])
{
    std::vector<char> x1 = build("The Quick Brown Fox Jumps Over The Lazy Dog");
    std::vector<char> x2 = build("Oh give me a home where the buffalo roam");

    std::vector<char> intersection = intersect(poll(x1), poll(x2));

    for (std::vector<char>::iterator i=intersection.begin();
            i != intersection.end(); ++i)
        std::cout << *i;

    std::cout << std::endl;

    return 0;
}

从你后面的问题来看,你实际上只关心26个字符:

std::bitset<26> in;
for (std::vector<char>::iterator it = first.begin(); it != first.end(); ++it) {
    in[*it - 'a'] = true;
}
for (std::vector<char>::iterator it = second.begin(); it != second.end(); ++it) {
    if (in[*it - 'a']) {
        result.push_back(*it);
        // this line is only needed if 'second' can contain duplicates
        in[*it - 'a'] = false;
    }
}

谢谢。我本以为会是这样的。@BillyONeal Kristo的回答在我留下评论时没有包括这一部分。@Jon Eric:事实上,它包括了。第一版有这些信息。如果你不相信我,请查看编辑历史。好了,伙计们,没必要争论。我通过连续几次编辑建立了我的答案。显然y、 SO引擎并没有将它们全部保存为单独的修订版。需要注意的是,
binary_search
需要对范围进行排序,因此手动实现仅在
s2
实际上已排序的情况下才有效。如果没有对范围进行排序,则可以使用
std::find
而不是
std::binary_search
哪一个会更慢(
O(n^2)
对于整个算法)但不需要排序。稍微修改了标题,因为在前一个版本中,它看起来像是您在寻找
std::vector::operator让我检查我理解这一点,因为我认为这有助于我理解我在发布问题后发现的关于集合交叉点的内容。inter包含b和c,它们是字符x和y共用的字符对吗?@Sam Phelps-是的,没错。cnt包含相交元素的数量(我只是把它放在这里,以防出于某种原因只需要获得相交元素的计数)。使用插入迭代器而不是为目标向量分配固定大小的数组可能更清晰。
assert(UCHAR_MAX <= 512 && "What kind of crazy machine is this?");
std::bitset<UCHAR_MAX> in;

BOOST_FOREACH(unsigned char c, first) {
    in[c] = true;
}

BOOST_FOREACH(unsigned char c, second) {
    if (in[c]) {
        result.push_back(c);
        // this line is only needed if 'second' can contain duplicates
        in[c] = false;
    }
}