gpt4 book ai didi

c++ - 将数组传回主体

转载 作者:行者123 更新时间:2023-11-30 03:30:27 25 4
gpt4 key购买 nike

我正在考虑网络中可用于我的优化项目的函数。我偶然发现了 Bateesh 提供的可用代码,我发现它非常有用。但我想稍微修改一下他的代码,这样

**不是打印输出,而是在我的主体中传输以用于下一个过程。

我该怎么做?我有新的 C++,所以任何建议都会有所帮助。我目前正在阅读有关传递数组的内容,但我对这些新想法感到茫然。

谢谢。

    // Program to print all combination of size r in an array of size n
#include <stdio.h>
#include <stdlib.h>
void combinationUtil(int arr[], int n, int r, int count, int data[], int i);

// Needed for qsort. See http://w...content-available-to-author-only...s.com/reference/cstdlib/qsort/
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}

// The main function that prints all combinations of size r
// in arr[] of size n. This function mainly uses combinationUtil()
void printCombination(int arr[], int n, int r)
{
// A temporary array to store all combination one by one
int data[r];

// Sort array to handle duplicates
qsort (arr, n, sizeof(int), compare);

// Print all combination using temprary array 'data[]'
combinationUtil(arr, n, r, 0, data, 0);
}

/* arr[] ---> Input Array
n ---> Size of input array
r ---> Size of a combination to be printed
index ---> Current index in data[]
data[] ---> Temporary array to store current combination
i ---> index of current element in arr[] */
void combinationUtil(int arr[], int n, int r, int index, int data[], int i)
{
// Current cobination is ready, print it
if (index == r)
{
for (int j=0; j<r; j++)
printf("%d ",data[j]);
printf("\n");
return;
}

// When no more elements are there to be put
if (i >= n)
return;

// current is included, put next at next location
data[index] = arr[i];
combinationUtil(arr, n, r, index+1, data, i+1);

// Remove duplicates
while (arr[i] == arr[i+1])
i++;

// current is excluded, replace it with next (Note that
// i+1 is passed, but index is not changed)
combinationUtil(arr, n, r, index, data, i+1);
}

// Driver program to test above functions
int main()
{
int arr[] = {1, 2, 1, 3, 1};
int r = 3;
int n = sizeof(arr)/sizeof(arr[0]);
printCombination(arr, n, r);
return 0;
}

最佳答案

完全不要使用数组

我不会回答你问的问题,因为我认为原始数组在大多数情况下都很糟糕。如果它们被传递(这就是您所要求的),则更是如此。

但是,C++ 确实提供了原始数组的替代方案(想到 std::arraystd::vector)。除非您自己编写容器,否则我建议优先使用这些而不是原始数组。它们的性能非常可靠。


请使用 C++ 标准库函数

如果我没理解错的话,您是在尝试显示给定数字集合中唯一值的所有可能排列。您可以使用一些 C++ 库函数来执行此操作:

#include <algorithm>
#include <iostream>
#include <vector>

int main()
{
// Create vector
std::vector<int> v { 1, 2, 1, 3, 1 };

// Sort vector
std::sort(v.begin(), v.end());

// Move all duplicate entries to the end of the vector
auto it = std::unique(v.begin(), v.end());

// Trim vector so that the duplicates are no longer contained
v.resize(std::distance(v.begin(), it));

// Iterate as long as the function can rearrange the objects as a
// lexicographicaly greater permutation
do
{
// Print all elements in the vector
for(auto i : v)
std::cout << i << " ";

// Add new line character and flush output
std::cout << std::endl;

} while(std::next_permutation(v.begin(),v.end()));

return 0;
}

输出:

1 2 3 
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1

关于c++ - 将数组传回主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44983257/

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