1 #include<iostream> 2 #include<vector> 3 #include<string> 4 using namespace std; 5 void combine(char *str, int begin, int m, vector<char>&vec) 6 { 7 if (m == 0)//当m为0时,表明字符数已经选够 8 { 9 vector<char>::iterator it = vec.begin(); 10 for (; it != vec.end(); it++) 11 cout << *it; 12 cout << endl; 13 return; 14 } 15 if (begin == strlen(str))//开始位置到达字符串 末尾时返回 16 return; 17 vec.push_back(str[begin]);//情况1是从剩余的字符中选择m-1个 18 combine(str, begin + 1, m - 1, vec); 19 vec.pop_back(); 20 combine(str, begin + 1, m, vec);//从全部的剩余字符中再选出m个 21 } 22 void combination(char *str, int m) 23 { 24 if (str==NULL || m <= 0 || m > strlen(str)) 25 return; 26 vector<char> vec; 27 28 combine(str, 0, m, vec); 29 30 } 31 32 int main() 33 { 34 char *str = "abcd"; 35 combination(str, 3); 36 cin.get(); 37 return 0; 38 }