【问题标题】:Concatenate Arrays and Sort them连接数组并对其进行排序
【发布时间】:2019-05-15 19:15:17
【问题描述】:

我正在尝试连接 2 个 int 数组,然后我尝试对结果数组进行排序。阅读效果很好,但我认为连接部分存在一些问题。如果有人能启发我,我会很高兴并原谅我的垃圾英语。注意:代码应保持在低 lvl 编程(如 C++ rookie-all code in main) 上面的代码:

int N, M,vect1[500],vect2[500];

cin>>N;
for(int i=0; i<N; i++)
    cin>>vect1[i];

cin>>M;
for(int i=0; i<M; i++)
    cin>>vect2[i];

int rez1 = sizeof(vect1) / sizeof(vect1[0]);
int rez2 = sizeof(vect2) / sizeof(vect2[0]);
int rez3=rez1+rez2;

vect1[N+rez3];
int j=0;
for(int i = rez1; i < rez3 ; i++`
{
        vect1[i]=vect2[j]; 
        j++;
}

int sortat = 0, aux;
while (sortat == 0)
 {
sortat = 1;
for (int i = 1; i < rez3; ++i)
  if (vect1[i] > vect1[i + 1])
    {
     sortat = 0;
     // interschimbam pe v[i] cu v[i + 1]
      aux = vect1[i];
      vect1[i] = vect1[i + 1];
      vect1[i + 1] = aux;
  }}                 for(int i=0; i <rez3; i++)
    cout<<vect1[i];       
return 0; 

【问题讨论】:

  • 听起来您可能需要学习如何使用调试器来单步调试您的代码。使用好的调试器,您可以逐行执行您的程序,并查看它与您期望的偏差在哪里。如果您要进行任何编程,这是必不可少的工具。进一步阅读:How to debug small programsDebugging Guide
  • 提示:查看sizeof(vect1) / sizeof(vect1[0])的结果,你会感到惊讶。
  • vect1[N+rez3];的意图是什么?我认为您需要在这里使用第三个数组来保存您的串联输出。
  • sizeof(vect1) / sizeof(vect1[0]) 的结果不是500 吗?这是你真正想要的吗?

标签: c++ arrays int concatenation


【解决方案1】:
template <class Type, size_t sizeA, size_t sizeB>
void concat_arrays(const Type(&a)[sizeA],
                  const Type(&b)[sizeB],
                  Type(&result)[sizeA + sizeB])
{
    Type(&midpoint)[sizeB] = (Type(&)[sizeB])(result[sizeA]);
    std::copy(a, a + sizeA, result);
    std::copy(b, b + sizeB, midpoint);
}

可以如下使用:

int main()
{
    int a[3] = { 1, 2, 3 };
    int b[3] = { 10, 20, 30 };
    int c[6];

    concat_arrays(a, b, c); // c = a . b

    size_t cSize = sizeof(c) / sizeof(*c);

    for (int i = 0; i < cSize; i++)
        std::cout << c[i] << std::endl;

    return 0;
}

【讨论】:

  • Type(&amp;result)[sizeA + sizeB] 有点限制。如果我想将它们组合成一个更大的数组,以后再添加更多数据怎么办?
  • 演员是迂腐的 UB,顺便说一句。只需使用Type* midPoint = result + sizeA;
  • 它应该保持在低水平(编程 lvl)它应该在 main 中解决,没有指针或函数,初学者的基本 C++
  • 对于@NathanOliver 的观点,在模板参数列表中添加另一个size_t,然后static_assert 它是&gt;= sizeA + sizeB
  • @PargeBeniamin:这些似乎是任意限制。此外,您的问题中没有提到它们
【解决方案2】:

std::vector 像 C 风格的数组一样使用,但没有那么多烦人的怪癖。

#include <iostream> // for std::cin, std::cout
#include <vector> // for std::vector
#include <algorithm> // for std::sort

int main()
{
    int N, M;
    std::cin >> N;

    std::vector<int> vect1(N); // Allocates N ints
    for(int i = 0; i != N; ++i)
        std::cin >> vect1[i]; // Indexed like an array

    std::vector<int> vect2(M); // Allocates M ints
    for(int i = 0; i != M; ++i)
        std::cin >> vect2[i];

    vect1.reserve(N + M); // Reallocate to fit more
    vect1.insert(vect1.end(), vect2.begin(), vect2.end()); // add elements at the back

    std::sort(vect1.begin(), vect1.end()); // All the algorithms operate on pairs of iterators

    for(int i = 0; i != M + N; ++i)
        std::cout << vect1[i];
}

【讨论】:

    猜你喜欢
    • 2021-10-15
    • 2011-12-26
    • 1970-01-01
    • 2021-12-08
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 2021-05-19
    • 2016-10-06
    相关资源
    最近更新 更多