C++ C++;处理函数重载的更好方法

C++ C++;处理函数重载的更好方法,c++,overloading,C++,Overloading,我有如下代码: void Foo(int& a, string& b, vector<int>& c) { ... // 30 lines of code are same as another function ... // 10 lines of code to parse and assign value to vector<int>& c } void Foo(int& a, string& b, map<s

我有如下代码:

void Foo(int& a, string& b, vector<int>& c) {
... // 30 lines of code are same as another function
... // 10 lines of code to parse and assign value to vector<int>& c
}

void Foo(int& a, string& b, map<string, int>& d) {
... // 30 lines of code are same as another function
... // 10 lines of code to parse and assign value to map<string, int>& d
}
void Foo(int&a、string&b、vector&c){
…//30行代码与另一个函数相同
…//10行代码,用于解析并为vector&c赋值
}
void Foo(int&a、string&b、map&d){
…//30行代码与另一个函数相同
…//10行代码,用于解析并为map&d赋值
}
有没有办法避免重复那30行代码?在这种情况下我应该使用函数重载吗



编辑:

如果代码不容易分离怎么办?比如:

void Foo(int& a, string& b, vector<int>& c) {
  for() {
    if(m) ... // 30 lines of code are same as another function
    else if(n) ... // 30 lines of code are same as another function
    else if(o) ... // 30 lines of code are same as another function
    else if(p) ... // 10 lines of 'vector<int>& c' code
    else if(q) ... // 10 lines of 'vector<int>& c' code
  }
}


void Foo(int& a, string& b, map<string, int>& d) {
  for() {
    if(m) ... // 30 lines of code are same as another function
    else if(n) ... // 30 lines of code are same as another function
    else if(o) ... // 30 lines of code are same as another function
    else if(p) ... // 10 lines of 'map<string, int>& d' code
    else if(q) ... // 10 lines of 'map<string, int>& d' code
  }
}
void Foo(int&a、string&b、vector&c){
for(){
如果(m)…//30行代码与另一个函数相同
如果(n)//30行代码与另一个函数相同,则为else
否则,如果(o)…//30行代码与另一个函数相同
else if(p)../10行“vector&c”代码
else if(q)..//10行“vector&c”代码
}
}
void Foo(int&a、string&b、map&d){
for(){
如果(m)…//30行代码与另一个函数相同
如果(n)//30行代码与另一个函数相同,则为else
否则,如果(o)…//30行代码与另一个函数相同
else if(p)…//10行“地图与设计”代码
如果(q)//10行“地图与设计”代码
}
}

将这30行代码重构为一个在两个重载中都调用的帮助函数


编辑:如果代码差异太大,以至于您很难将其分开,那么问题出在哪里?

您可以找出常见代码:

void helper(int& a, string& b) { 
  ... // 30 lines of common code
} 
然后在函数中使用:

void Foo(int& a, string& b, vector<int>& c) {     
  helper(a, b);
  ... // 10 lines of code to parse and assign value to vector<int>& c     
}     

void Foo(int& a, string& b, map<string, int>& d) {     
   helper(a, b);
.  .. // 10 lines of code to parse and assign value to map<string, int>& d     
}
voidfoo(int&a,string&b,vector&c){
助理(甲,乙);;
…//10行代码,用于解析并为vector&c赋值
}     
void Foo(int&a、string&b、map&d){
助理(甲,乙);;
…//10行代码,用于解析并为map&d赋值
}
或者,如果通用代码也包含对容器的引用,则可以使用模板:

template<template<typename T, typename Alloc> class Container>
void helper(int& a, string& b, Container& d) { 
  ... // 30 lines of common code
} 
模板
void助手(int&a、string&b、Container&d){
…//30行通用代码
} 
注意:您必须使用模板专门化,因为并非所有容器都具有相同的插入(或访问)方法(例如,向量、列表:
推回
;映射:
插入

更新:OP之后向问题添加了更多代码:


如果唯一的区别在于容器的处理,但是容器处理的“精神”非常相似,那么您可以创建(模板)容器的包装器并将包装器传递到一个公共函数中:差异将在包装器的不同实现中捕获。

也许大多数公共内容都可以通过迭代器处理?

为什么不将公共30行放在一个单独的函数中?将30行公共代码放在一个单独的函数中,由
Foo
函数调用的?