正在查找数组的大小

Finding the size of an array

本文关键字:数组 查找      更新时间:2023-10-16

程序的思想是在数组中输入元素。然后给整数"x"一个值。如果'x'是3,并且数组a[]包含元素{1,2,3,4,5,6},我们必须将a[]"拆分"为另外两个数组。比方说b[]和c[]。在b[]中,我们必须将所有值都设置为小于或等于3,在c[]中,所有值都大于3。

我的问题是-我如何表达b[i]中的3个元素?

#include <iostream>
using namespace std;
int main()
{
    int a[6];
    int b[6];
    int c[6];
    int d;

    for (int i = 0; i < 6; i++) {
        cin >> a[i];
    }
    cin >> d;
    for (int i = 0; i < 6; i++) {
        if (d >= a[i]) {
            b[i] = a[i]; // if d is 3, then i have 3 elements. How can i express them?
        }
    }
    for (int i = 0; i < 6; i++) {
        if (d< a[i]) {
            c[i] = a[i];
        }
    }
    for (int i = 0; i < 3; i++) {
        cout << b[i];
    }
    cout << endl;
    for (int i = 3; i < 6; i++) {
        cout << c[i];
    }

    return 0;
}

我认为您所要做的就是确定从a[]复制到b[]c[]int值的数量。要做到这一点,再引入两个计数器,它们从零开始,并随着每个项目复制到相关数组而递增:

类似这样的东西:

#include <iostream>
using namespace std;
int main()
{
    int a[6];
    int b[6], b_count=0; // see here
    int c[6], c_count=0; // see here
    int d;

    for (int i = 0; i < 6; i++) {
        cin >> a[i];
    }
    cin >> d;
    for (int i = 0; i < 6; i++) {
        if (d >= a[i]) {
            b[b_count++] = a[i]; // see here
        }
    }
    for (int i = 0; i < 6; i++) {
        if (d< a[i]) {
            c[c_count++] = a[i]; // see here
        }
    }
    for (int i = 0; i < b_count; i++) { // see here
        cout << b[i];
    }
    cout << endl;
    for (int i = 3; i < c_count; i++) { // and finally here
        cout << c[i];
    }

    return 0;
}

现在,如果您希望b[]c[]在其空间分配中是动态,那么像st::vector<>这样的动态托管容器会很有用,但我认为这对于特定于任务是不需要的。如果需要,您的b[]c[]已经足够大,可以容纳a[]中的所有元素。

WhozCraigs的答案很好地展示了根据任务需求使用传统数组解决此问题所需的内容。

我只是想向你展示,如果你被允许使用标准库的全部武器库,这是如何做到的。这就是为什么人们呼吁你使用std::vector。这样事情就变得更简单了。

#include <algorithm>
#include <iostream>
int main()
{
    int a[6] = {1, 2, 3, 4, 5, 6 }; // Not using input for brevity.
    int x = 3; // No input, for brevity
    // Lets use the std:: instead of primitives
    auto first_part = std::begin(a);
    auto last = std::end(a);
    auto comparison = [x](int e){ return e <= x; };
    auto second_part = std::partition(first_part, last, comparison);
    // Print the second part. 
    std::for_each(second_part, last, [](int e){ std::cout << e; });
    // The first part is first_part -> second_part
}

分区函数执行的正是您的问题要求您解决的问题,但它是在数组a内部执行的。返回的值是第二部分中的第一个元素。

使用std::vectors。不要使用int[] s。

对于int[]s(在c++11之前),您可以通过一些严格的假设,找到sizeof(X)/sizeof(X[0])的数组长度;然而,这从来都不是一个好的做法。

在您提供的示例中,您可能想要:

    #define MAX_LEN 100
    ...
    int main() {
       int a[MAX_LEN];
       int b[MAX_LEN];
       int c[MAX_LEN];
       int n;
       std::cout << "how many elements do you want to read?" << std::endl;
       std::cin >> n;

从那时起使用n(这是编程学校的常见做法)

考虑一个读取int向量的函数:

std::vector<int> readVector() {
    int n;
    std::cout << "how many elements do you want to read?" << std::endl;
    std::cin >> n;
    std::vector<int> ret;
    for (int i=0; i<n; i++) {
        std::cout << "please enter element " << (i+1) << std::endl;
        int el;
        std::cin >> el;
        ret.push_back(el);
    }
    return ret;
}

您可以使用,在主要情况下,auto a = readVector(); auto b = readVector(); a.size()将是长度,并允许保留任何数量的int

这里有一个例子,说明一旦你有了更多的经验,你将如何处理它。

任何你在这里不理解的东西都值得在这里学习:

#include <iostream>
#include <vector>
#include <utility>
std::vector<int> get_inputs(std::istream& is)
{
    std::vector<int> result;
    int i;
    while(result.size() < 6 && is >> i) {
        result.push_back(i);
    }
    return result;
}
std::pair<std::vector<int>, std::vector<int>>
split_vector(const std::vector<int>& src, int target)
{
    auto it = std::find(src.begin(), src.end(), target);
    if (it != src.end()) {
        std::advance(it, 1);
    }
    return std::make_pair(std::vector<int>(src.begin(), it),
                          std::vector<int>(it, src.end()));
}
void print_vector(const std::vector<int>& vec)
{
    auto sep = " ";
    std::cout << "[";
    for (auto i : vec) {
        std::cout << sep << i;
        sep = ", ";
    }
    std::cout << " ]" << std::endl;
}
int main()
{
    auto initial_vector = get_inputs(std::cin);
    int pivot;
    if(std::cin >> pivot)
    {
        auto results = split_vector(initial_vector, pivot);
        print_vector(results.first);
        print_vector(results.second);
    }
    else
    {
        std::cerr << "not enough data";
        return 1;
    }

    return 0;
}

示例输入:1 2 3 4 5 63

预期输出:

[ 1, 2, 3 ]
[ 4, 5, 6 ]