【问题标题】:Finding the size of an array查找数组的大小
【发布时间】:2016-04-14 15:03:45
【问题描述】:

程序的想法是在数组中输入元素。然后给整数“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;
}

【问题讨论】:

  • 你的意思是你想要像std::vector这样的东西?
  • 我们还没有研究过向量。我知道 c[i] = 4,5,6 和 b[i]=1,2,3 ,如果 d =3。它们都包含 3 个元素,但我不知道如何表达。
  • 如果您在学习向量之前学习 c 数组,那么您就不是在学习 c++。请不要相信你的老师,请阅读向量。
  • 我不清楚其他人在喋喋不休。据我所知,在填充上下数组时,您似乎需要使用一对额外的计数器,这些计数器的大小已经方便地调整为最大 潜在 使用量。当您为给定数组选择元素时,将其从 a[] 复制过来,然后碰撞目标数组的计数器。
  • @specbk,我建议考虑如何压缩代码以避免在开始时必须对数组进行两次迭代以填充 b[]c[]。你们已经了解了if 语句,所以我相信你们也了解了可以与之配套的else 块。如果没有别的,这对你来说可能是一个很好的练习。

标签: c++ arrays for-loop


【解决方案1】:

我认为您要做的就是确定您将多少个int 值从a[] 复制到b[]c[]。为此,请引入另外两个计数器,它们从零开始并随着复制到关联数组的每个项目而递增:

类似这样的:

#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&lt;&gt; 这样的动态管理容器会很有用,但我不认为那此特定任务需要。您的b[]c[] 已经足够大,可以容纳a[] 中的所有 元素(如果需要)。

【讨论】:

  • 为什么不用else语句将2个循环合二为一?
  • @Slava 因为那不是问题所在。这家伙像草一样绿;越少吸收越好。
  • 一个循环将清楚地实现意图 - 将 a[] 复制到 b[] 或 c[] - 这使代码更清晰,更易于理解。至少应该提到它。
  • 非常感谢!我不知道我可以以这种方式使用计数器。 @Slava 我会试试的,谢谢!
【解决方案2】:

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::begin(a)std::end(a),而不是幻数
  • @Slava 确实如此。谢谢。
【解决方案3】:

使用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(这些是编程学校的常见做法)

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

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() 作为长度,并允许保留任意数量的整数

【讨论】:

  • 我认为我们都同意向量是比数组更好的 C++ 解决方案,但这个问题专门针对数组。它没有提到向量
【解决方案4】:

这里有一个示例,说明您在获得更多经验后将如何处理它。

这里有不懂的都值得学习here

#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 6 3

预期输出:

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

【讨论】:

  • 等我读完关于向量的文章我会试试看,非常感谢!
猜你喜欢
  • 1970-01-01
  • 2023-04-07
  • 2012-10-19
  • 2011-11-16
  • 2021-06-07
  • 2012-05-04
  • 1970-01-01
  • 2013-04-07
  • 2021-01-15
相关资源
最近更新 更多