查找数组的大小

时间:2016-04-14 15:03:46

标签: c++ arrays for-loop

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

4 个答案:

答案 0 :(得分:2)

我认为您所做的一切都是确定您从int复制到a[]或{{}的b[]个值的方法1}}。为此,请引入另外两个从零开始的计数器,并将每个项目复制到关联的数组:

这样的事情:

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<>b[]已足够大,可以在需要时保留c[]中的所有元素。

答案 1 :(得分:1)

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内完成。返回的值是第二部分中的第一个元素。

答案 2 :(得分:0)

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

使用int[] s(也就是pre-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;
}

你可以使用,在{main} auto a = readVector(); auto b = readVector(); a.size()中,它将是长度,并允许保留任意数量的整数

答案 3 :(得分:0)

这是一个例子,说明一旦你获得更多经验,你将如何接近它。

你在这里不理解的任何事情都值得研究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 ]