C++ 如何在C+中比较两个数组并返回不匹配的值+;
我想分析两个字符串向量,找出彼此匹配的字符串和不匹配的字符串 我想要得到的示例:C++ 如何在C+中比较两个数组并返回不匹配的值+;,c++,vector,C++,Vector,我想分析两个字符串向量,找出彼此匹配的字符串和不匹配的字符串 我想要得到的示例: 输入向量1看起来像:[string1,string2,string3] 输入向量2看起来像:[string2,string3,string4] 理想输出: string1:不匹配 字符串2:匹配 字符串3:匹配 第四条:没有对手 目前,我使用以下代码: vector<string> function(vector<string> sequences, vector<string>
输入向量1看起来像:[string1,string2,string3]
输入向量2看起来像:[string2,string3,string4] 理想输出:
string1:不匹配
字符串2:匹配
字符串3:匹配
第四条:没有对手 目前,我使用以下代码:
vector<string> function(vector<string> sequences, vector<string> second_sequences){
for(vector<string>::size_type i = 0; i != sequences.size(); i++) {
for(vector<string>::size_type j = 0; j != second_sequences.size(); j++){
if (sequences[i] == second_sequences[j]){
cout << "Match: " << sequences[i];
}else{
cout << "No Match: " << sequences[i];
cout << "No Match: " << second_sequences[j];
}
}
}
}
向量函数(向量序列,向量二次_序列){
对于(vector::size_type i=0;i!=sequences.size();i++){
对于(vector::size_type j=0;j!=second_sequences.size();j++){
if(序列[i]==第二个_序列[j]){
cout最好的代码是您不必编写的代码
如果使用(STL)映射容器,它将负责排序和记忆遇到的不同字符串
所以让容器为我们工作吧
我建议快速编写一个小代码。您需要使用这种语法来至少启用编译器的C++ 2011选项(例如,在GCC上,STD= C++ 11)。在C++ 11之前应该使用的语法更冗长(但应该从学者的角度来理解)。
您只有一个循环。
这只是给您的一个提示(我的代码没有考虑到在第二个向量中,string4可能会出现多次,但我允许您根据您的具体需要进行安排)
std::sort(std::begin(v1)、std::end(v1));
std::sort(std::begin(v2),std::end(v2));
std::向量公共元素;
标准::设置交叉点(标准::开始(v1),标准::结束(v1)
,std::begin(v2),std::end(v2)
,std::back_inserter(公共_元素));
用于(自动常量&s:常用元素)
{
std::我认为i=i++
应该是++i
(与j
obvs相同)。此外,对第一个数组进行排序,并对第二个数组进行二进制搜索作为输入,这将提高您的结果。这可以通过排序和使用set_交叉和set_对称_差异来实现。请参见此处:我链接的实现不仅工作正常,而且在时间复杂度上比您尝试的双嵌套循环更快g开始工作。底线是——如果您正在编写一个听起来像以前完成过的函数,那么很可能有算法函数可以完成这项工作。在两个序列中查找不匹配或相同的项是经常做的事情,因此有算法函数可以执行这类任务。请参阅:
#include <iostream>
#include <vector>
#include <string>
#include <map>
using namespace std;
vector<string> v1 { "string1","string2","string3"};
vector<string> v2 { "string2","string3","string4"};
//ordered map will take care of "alphabetical" ordering
//The key are the strings
//the value is a counter ( or could be any object of your own
//containing more information )
map<string,int> my_map;
int main()
{
cout << "Hello world!" << endl;
//The first vector feeds the map before comparison with
//The second vector
for ( const auto & cstr_ref:v1)
my_map[cstr_ref] = 0;
//We will look into the second vector ( it could also be the third,
//the fourth... )
for ( const auto & cstr_ref:v2)
{
auto iterpair = my_map.equal_range(cstr_ref);
if ( my_map.end() != iterpair.first )
{
//if the element already exist we increment the counter
iterpair.first->second += 1;
}
else
{
//otherwise we put the string inside the map
my_map[cstr_ref] = 0;
}
}
for ( const auto & map_iter: my_map)
{
if ( 0 < map_iter.second )
{
cout << "Match :";
}
else
{
cout << "No Match :" ;
}
cout << map_iter.first << endl;
}
return 0;
}
No Match :string1
Match :string2
Match :string3
No Match :string4
std::sort(std::begin(v1), std::end(v1));
std::sort(std::begin(v2), std::end(v2));
std::vector<std::string> common_elements;
std::set_intersection(std::begin(v1), std::end(v1)
, std::begin(v2), std::end(v2)
, std::back_inserter(common_elements));
for(auto const& s : common_elements)
{
std::cout<<s<<std::endl;
}