关于不确定长度数组的遍历组合

给定一个集合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;
}

这里写图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值