日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

向量笛卡尔积_如何创建向量的矢量的笛卡尔积?

發布時間:2025/3/21 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 向量笛卡尔积_如何创建向量的矢量的笛卡尔积? 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

首先,我將向您展示一個遞歸版本。

// Cartesion product of vector of vectors

#include

#include

#include

// Types to hold vector-of-ints (Vi) and vector-of-vector-of-ints (Vvi)

typedef std::vector Vi;

typedef std::vector 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<

{

os << "(";

std::copy(vi.begin(), vi.end(), std::ostream_iterator(os, ", "));

os << ")";

return os;

}

std::ostream&

operator<

{

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偷走的遞歸迭代版本:

程序的其余部分幾乎相同,只顯示了cart_product功能。

// Seems like you'd want a vector of iterators

// which iterate over your individual vectors.

struct Digits {

Vi::const_iterator begin;

Vi::const_iterator end;

Vi::const_iterator me;

};

typedef std::vector 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;

}

}

}

}

總結

以上是生活随笔為你收集整理的向量笛卡尔积_如何创建向量的矢量的笛卡尔积?的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。