C++ 模板函数,用于查找将字符串的一部分作为指针写入的多重映射相交,其余部分则正确写入
我已经编写了一个代码,用于查找两个多重映射的交点,这两个多重映射将进行比较 与键相关的值,并获取包含逗号元素(组合)的映射 键和值)从两个映射。这是将字符串的一部分显示为指针 像往常一样休息C++ 模板函数,用于查找将字符串的一部分作为指针写入的多重映射相交,其余部分则正确写入,c++,C++,我已经编写了一个代码,用于查找两个多重映射的交点,这两个多重映射将进行比较 与键相关的值,并获取包含逗号元素(组合)的映射 键和值)从两个映射。这是将字符串的一部分显示为指针 像往常一样休息 #include <string> #include <iostream> #include <map> #include <iterator> #include <stdio.h> template <class InputIterato
#include <string>
#include <iostream>
#include <map>
#include <iterator>
#include <stdio.h>
template <class InputIterator1, class InputIterator2, class OutputIterator>
/* templet function to return common elements od two map */
OutputIterator map_intersection ( InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result )
{
while (first1!=last1 && first2!=last2)
{
if (*first1<*first2) ++first1;
else if (*first2<*first1) ++first2;
else {
const char*abc= new char[15];
const char*def= new char[15];
/* Below code inserts the common element in result map */
if(strcmp((first1->second.c_str()),(first2->second.c_str()))==0)
{
result.insert(std::pair<std::string,std::string>(first1->first,first1->second));
++first1;
++first2;
}
else
{
++first1;
++first2;
}
}
}
return result;
}
using namespace std;
int main()
{
std::multimap<string, string> m;
std::multimap<string, string>::iterator it2;
std::multimap<string, string> intersection;
std::multimap<string, string> common;
std::multimap<string, string> it3;
cout <<"Making Map m "<<endl;
m.insert(pair< string, string>("1 2"," 22 3" ));
m.insert(pair< string, string>("1 2"," 21 4" ));
m.insert(pair< string, string>("2 1"," 31 3" ));
cout <<"Making Map c "<<endl;
std::multimap<string, string> c;
c.insert(pair< string, string>("1 2"," 22 3" ));
c.insert(pair< string, string>("1 2"," 22 4" ));
c.insert(pair< string, string>("2 1"," 31 3" ));
cout << "Elements in common map are: " << endl;
it3=map_intersection (m.begin(), m.end(), c.begin(), c.end(),common);
cout << " am i out of the map_intersection loop " << endl;
cout << " size of common map is : " << it3.size()<< endl;
for(it2 =it3.begin(); it2!=it3.end();it2++)
{
cout << "first common element is : " <<
cout << it2->first << " " << it2->second <<" " << endl;
}
getchar();
}
控制台上的输出:
0x4483c41 2 22 3
0x4483c42 1 31 3
从您的代码:
cout << "first common element is : " <<
cout << it2->first << " " << it2->second <<" " << endl;
^^^^
“代码> CUT”模板函数,用于查找字符串的一个部分,作为指针和静止正确地写入一个部分。”StAcExcel应该提供最复杂的铜牌,同时也提供零信息内容标题。NoTiPoice:C++实际上不知道什么是“模板函数”。(如果我没记错的话,即使是标准也偶然提到了它。)你是指“函数模板”.有一个很小但基本的概念上的区别。有一段时间以前,看看。谢谢interjay,那是我忽略的一个错误。谢谢lot@mikeNakis哥们,每个人都不像你一样是莎士比亚,但我更愿意在写标题时提供更多信息,如果给你带来不便,我很抱歉。@konard Rudolph谢谢你努力。。Regards@kerrek某人…谢谢你的努力…问候
cout << "first common element is : " <<
cout << it2->first << " " << it2->second <<" " << endl;
^^^^