gpt4 book ai didi

c++ - 如何创建 vector vector 的笛卡尔积?

转载 作者:IT老高 更新时间:2023-10-28 21:49:45 25 4
gpt4 key购买 nike

我有一个 vector ,比如 vector<vector<int> > items不同尺寸如下

1,2,3
4,5
6,7,8

我想根据这些 vector 的笛卡尔积来创建组合,例如

1,4,6
1,4,7
1,4,8
and so on till
3,5,8

我该怎么做?我查看了几个链接,并在这篇文章的末尾列出了它们,但我无法解释,因为我对这种语言不太熟悉。有人可以帮我解决这个问题。

#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

int main()
{
vector<vector<int> > items;
int k = 0;

for ( int i = 0; i < 5; i++ ) {
items.push_back ( vector<int>() );

for ( int j = 0; j < 5; j++ )
items[i].push_back ( k++ );
}

cartesian ( items ); // I want some function here to do this.
}

这个程序有相等长度的 vector ,我把它放在这里是为了更容易理解我的数据结构。即使有人使用其他链接中的其他答案并与之集成以获得结果,这也会非常有帮助。非常感谢

我看过的几个链接 one Two程序来自:program

最佳答案

首先,我将向您展示一个递归版本。

// Cartesion product of vector of vectors

#include <vector>
#include <iostream>
#include <iterator>

// Types to hold vector-of-ints (Vi) and vector-of-vector-of-ints (Vvi)
typedef std::vector<int> Vi;
typedef std::vector<Vi> Vvi;

// Just for the sample -- populate the intput data set
Vvi build_input() {
Vvi vvi;

for(int i = 0; i < 3; i++) {
Vi vi;
for(int j = 0; j < 3; j++) {
vi.push_back(i*10+j);
}
vvi.push_back(vi);
}
return vvi;
}

// just for the sample -- print the data sets
std::ostream&
operator<<(std::ostream& os, const Vi& vi)
{
os << "(";
std::copy(vi.begin(), vi.end(), std::ostream_iterator<int>(os, ", "));
os << ")";
return os;
}
std::ostream&
operator<<(std::ostream& os, const Vvi& vvi)
{
os << "(\n";
for(Vvi::const_iterator it = vvi.begin();
it != vvi.end();
it++) {
os << " " << *it << "\n";
}
os << ")";
return os;
}

// recursive algorithm to to produce cart. prod.
// At any given moment, "me" points to some Vi in the middle of the
// input data set.
// for int i in *me:
// add i to current result
// recurse on next "me"
//
void cart_product(
Vvi& rvvi, // final result
Vi& rvi, // current result
Vvi::const_iterator me, // current input
Vvi::const_iterator end) // final input
{
if(me == end) {
// terminal condition of the recursion. We no longer have
// any input vectors to manipulate. Add the current result (rvi)
// to the total set of results (rvvvi).
rvvi.push_back(rvi);
return;
}

// need an easy name for my vector-of-ints
const Vi& mevi = *me;
for(Vi::const_iterator it = mevi.begin();
it != mevi.end();
it++) {
// final rvi will look like "a, b, c, ME, d, e, f"
// At the moment, rvi already has "a, b, c"
rvi.push_back(*it); // add ME
cart_product(rvvi, rvi, me+1, end); add "d, e, f"
rvi.pop_back(); // clean ME off for next round
}
}

// sample only, to drive the cart_product routine.
int main() {
Vvi input(build_input());
std::cout << input << "\n";

Vvi output;
Vi outputTemp;
cart_product(output, outputTemp, input.begin(), input.end());
std::cout << output << "\n";
}

现在,我将向您展示我无耻地从@John 那里窃取的 recursive 迭代版本:

程序的其余部分几乎相同,仅显示 cart_product 函数。

// Seems like you'd want a vector of iterators
// which iterate over your individual vector<int>s.
struct Digits {
Vi::const_iterator begin;
Vi::const_iterator end;
Vi::const_iterator me;
};
typedef std::vector<Digits> Vd;
void cart_product(
Vvi& out, // final result
Vvi& in) // final result

{
Vd vd;

// Start all of the iterators at the beginning.
for(Vvi::const_iterator it = in.begin();
it != in.end();
++it) {
Digits d = {(*it).begin(), (*it).end(), (*it).begin()};
vd.push_back(d);
}


while(1) {

// Construct your first product vector by pulling
// out the element of each vector via the iterator.
Vi result;
for(Vd::const_iterator it = vd.begin();
it != vd.end();
it++) {
result.push_back(*(it->me));
}
out.push_back(result);

// Increment the rightmost one, and repeat.

// When you reach the end, reset that one to the beginning and
// increment the next-to-last one. You can get the "next-to-last"
// iterator by pulling it out of the neighboring element in your
// vector of iterators.
for(Vd::iterator it = vd.begin(); ; ) {
// okay, I started at the left instead. sue me
++(it->me);
if(it->me == it->end) {
if(it+1 == vd.end()) {
// I'm the last digit, and I'm about to roll
return;
} else {
// cascade
it->me = it->begin;
++it;
}
} else {
// normal
break;
}
}
}
}

关于c++ - 如何创建 vector vector 的笛卡尔积?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5279051/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com