【问题标题】:C++ function to return same-index elements of two char arraysC++ 函数返回两个 char 数组的相同索引元素
【发布时间】:2019-11-25 06:05:40
【问题描述】:

我知道这应该如何在我的脑海中发挥作用,但由于某种原因我无法对其进行编码。

我需要编写一个 c++ 函数,它接受两个字符数组并返回它们的“叉积”(不是乘法)组合。

string crossProd(const char* a, const char* b, int sizeofa, int sizeofb)

示例: 输入:a-->"abc", b-->"123" 输出:a1b2c3

感谢您的帮助。

【问题讨论】:

  • 你试过什么?什么不起作用?
  • 此外,a1b2c3cross-product 无关,您只是将两个字符串折叠在一起。
  • @DavidRankin-ReinstateMonica 这不是如何定义“交叉产品”的问题吗? SO: Cross Concatenate Elements of 2 Vectors to Produce a Third ;-)
  • A cross-product 是向量运算(如在物理向量中,而不是 C++ 动态模板数组)a, b, c1, 2, 3 的向量叉积将导致 (b3-c2)i + (c1-a3)j + (a2-1b)k 在 3-维度空间,而不是"a1b2c3",向量结果将与1,2,3a,b,c 正交,并且与原始两者形成的平面的法线 正交。

标签: c++ arrays function character


【解决方案1】:

试试这个:

#include <iostream>
#include <string.h>

std::string crossProd( const char* a, const char* b, int sizeofa, int sizeofb)
{
    std::string return_str = "";

    if (sizeofb >= sizeofa)
    {
        for (int i=0; i<=sizeofa; i++)
        {
            return_str = return_str + a[i] +b[i];
        }

        for(int i=sizeofa+1; i<=sizeofb; i++)
        {
            return_str = return_str + b[i];
        }

    }
    else
    {
        for (int i=0; i<=sizeofb; i++)
        {
            return_str = return_str + a[i] +b[i];
        }
        for(int i=sizeofb+1; i<=sizeofa; i++)
        {
            return_str = return_str + a[i];
        }
    }
    return return_str;
}

int main()
{
    const char* a = "Chitta";
    const char* b = "123456";
    std::cout<<a<<":"<<b<<":"<<crossProd(a,b,6,6)<<std::endl;

    const char* a1 = "ChittaRanjan";
    const char* b1 = "12345";
    std::cout<<a1<<":"<<b1<<":"<<crossProd(a1,b1,12,5)<<std::endl;

    const char* a2 = "Chitta";
    const char* b2 = "12345678";
    std::cout<<a2<<":"<<b2<<":"<<crossProd(a2,b2,6,8)<<std::endl;
    return 0;
}

输出:

  • 奇塔:123456:C1h2i3t4t5a6

  • ChittaRanjan:12345:C1h2i3t4t5aRanjan

  • 奇塔:12345678:C1h2i3t4t5a678

注意: 这不是优化的代码。

【讨论】:

  • Chitta Ranjan 非常感谢您!这是工作。我将分析代码,看看它是如何运行的以及我做错了什么。再次感谢! :)
  • np :) @princeofdhump
【解决方案2】:

您对cross-product 一词的使用有点含糊。 Cross product 是对 3 维空间中两个向量的二元运算。您显示为“交叉生成”的结果只是通过交替每个字符串中的字符将两个字符串折叠在一起,直到字符用完。

@ChittaRajan 给你一个很好的答案,但是有很多方法可以解决这个问题。一方面,您可以简单地使用operator[] 和索引来组合每个字符串中的常见字符,然后添加剩余的较长的字符,例如:

std::string fold (const std::string& a, const std::string& b)
{
    std::string s {};
    size_t i = 0, j;

    while (a[i] && b[i]) {  /* alternating add from a & b */
        s += a[i];
        s += b[i++];
    }
    j = i;

    while (a[i])            /* if a longer, add remaining chars */
        s += a[i++];
    while (b[j])
        s += b[j++];        /* if b longer, add remaining chars */

    return s;
}

既然你已经使用了基本的char* 类型,你可以简单地为每个字符串使用一个指向原始字符.c_str() 的指针和一对指针来完成相同的事情,例如

std::string fold (const std::string& a, const std::string& b)
{
    std::string s {};
    const char *pa = a.c_str(), *pb = b.c_str();

    while (*pa && *pb) {    /* alternating add from a & b */
        s += *pa++;
        s += *pb++;
    }
    while (*pa)             /* if a longer, add remaining chars */
        s += *pa++;
    while (*pb)
        s += *pb++;         /* if b longer, add remaining chars */

    return s;
}

在这两种情况下,当与一个简短的例子配对时:

int main (void) {

    std::string a {"abc"}, b {"123"}, c = fold (a, b);

    std::cout << c << '\n';
}

使用/输出示例

你收到了欲望输出:

$ ./bin/strfold
a1b2c3

查看两者,如果您还有其他问题,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-02
    • 1970-01-01
    • 2010-12-01
    • 2011-12-14
    • 2011-01-20
    • 1970-01-01
    • 2021-11-08
    相关资源
    最近更新 更多