如果我想得到这两个的笛卡尔积vector<string>
s:
vector<string> final{"a","b","c"};
vector<string> temp{"1","2"};
但我想把结果放进去final
,这样最终将包含:
a1
a2
b1
b2
c1
c2
我想在不创建临时数组的情况下执行此操作。是否有可能做到这一点?如果重要的话,顺序final
并不重要。
您可以尝试以下方法
#include <iostream>
#include <vector>
#include <string>
int main()
{
std::vector<std::string> final{ "a", "b", "c" };
std::vector<std::string> temp{ "1", "2" };
auto n = final.size();
final.resize( final.size() * temp.size() );
for ( auto i = n, j = final.size(); i != 0; --i )
{
for ( auto it = temp.rbegin(); it != temp.rend(); ++it )
{
final[--j] = final[i-1] + *it;
}
}
for ( const auto &s : final ) std::cout << s << ' ';
std::cout << std::endl;
return 0;
}
程序输出是
a1 a2 b1 b2 c1 c2
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)