关于不确定长度数组的遍历组合
给定一个集合List = {{“a1,b1,c1”},{“a2”“b2”“c2”}},数量不确定.求字符串组合的集合.比如(“a1a2”,”b1a2”)。。。
基本思路:用c++的基本数据结构就可以做出来.写一个函数每次把两个字符串集合遍历组合所得的结果计算出来.然后再写一个函数不停的遍历list里面的集合,并将前面的结果与新的元素再次计算.直到集合遍历完.
#include "iostream"
#include "string"
#include "vector"
using namespace std;
using S = vector<string>;//定义别名
S concat(const S &lh1,const S & lh2)//解决两个S类型集合的组合
{
S result;
for (auto &it1 : lh1)
for (auto &it2 : lh2)
result.push_back(it1 + it2);//存入一次字符串的组合
return result;
}
S accumulate(vector<S> List)
{
S result = List.front();
for (auto it = List.begin() + 1; it != List.end();++it)
result = concat(result, *it);//进行一次组合
return result;
}
int main()
{
string s;
vector<S> List;
while (getline(cin,s))
{
S temp;
for (auto it = s.begin();it!=s.end();++it)
{
string temps;
while (it!=s.end()&&!isspace(*it))//遇到一个不是空格的时候
temps.push_back(*it++);
temp.push_back(temps);
if (it == s.end())
break;
}
List.push_back(temp);
}
S result;
result = accumulate(List);
for (auto r : result)
cout << r<<" ";
return 0;
}
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇: 将整数数组进行m等分,使得每一个部分的和相等且m最大
- 下一篇:没有了