数组作为参数

发布于 2024-10-04 03:35:53 字数 184 浏览 0 评论 0原文

我创建了一个数组:

CString* pstrArray = new CString[nMySize];

现在如何将它传递给要填充的函数?实际参数是多少?

void FillThisArray(what goes here?)
{
}

I have created an array:

CString* pstrArray = new CString[nMySize];

Now how can I pass it to a function to be filled up? What is the actual parameter?

void FillThisArray(what goes here?)
{
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

生生漫 2024-10-11 03:35:53

您应该使用容器类: CStringArray

void FillThisArray( CStringArray & rMyStrings )

如果你不想要这个(我没有看到任何可能的原因,但无论如何):

void FillThisArray(CString* strings, size_t num)
{
  // example
  for( size_t s=0; s<num; s++ )
  {
    strings[s].Format( _T("This is the %d. string"), s+1 );
  }
}

You should use a container class: CStringArray

void FillThisArray( CStringArray & rMyStrings )

If you don't want this (I don't see any possible reason, but anyway):

void FillThisArray(CString* strings, size_t num)
{
  // example
  for( size_t s=0; s<num; s++ )
  {
    strings[s].Format( _T("This is the %d. string"), s+1 );
  }
}
唐婉 2024-10-11 03:35:53

CString* pstrArray = NULL; pstrArray = new CString[nMySize];

为了简单起见:

CString* pstrArray = new CString[nMySize];

现在我如何将它传递给要填充的函数?实际参数是什么?
无效 FillThisArray(????) { }

最明显的接口是:

void FillThisArray(CString* pstrArray, size_t n)

退后一步:

  • 单个新语句分配
  • 请注意,nMySize 默认构造的 CString 的所有内存都将由您的 应该考虑使用 std::vector
    • std::vector 因为:
      • 超出范围时自动删除所有字符串的内存
      • 默认情况下,随着使用 push_back() 添加字符串,内存使用量会逐渐增加,并且在这种情况下,内存使用量可能会超出初始大小,而无需您进行任何特殊工作
      • 如果您确实想要这种行为,您可以在构造向量时主动为 nMySize 字符串预留空间和/或创建它们
    • std::string 因为:
      • 它是 C++ 标准定义的可移植字符串类型,可减少对依赖项的锁定
      • 也就是说,在某些情况下避免这样做可能不切实际或效率低下

CString* pstrArray = NULL; pstrArray = new CString[nMySize];

For simplicity:

CString* pstrArray = new CString[nMySize];

Now how can i pass it to a function to be filled up? What is the actual parameter?
void FillThisArray(????) { }

The most obvious interface is:

void FillThisArray(CString* pstrArray, size_t n)

Taking a step back:

  • be aware that all the memory for nMySize default-constructed CStrings will be allocated by that single new statement
  • you should consider using a std::vector<std::string>
    • std::vector because:
      • automatically deletes the memory for all the strings when it goes out of scope
      • by default the memory usage will increase more gradually as strings are added using e.g. push_back(), and in such usage can grow beyond the initial size without any special work on your part
      • you can proactively reserve space for nMySize strings, and/or create them, when you construct the vector if you really want that behaviour
    • std::string because:
      • it's the portable string type defined by the C++ Standard, and reduces lock in to your dependencies
      • that said, it may be impractical or inefficient to avoid in some circumstances
傲鸠 2024-10-11 03:35:53

如果有充分的理由不能使用标准容器类,请考虑采用迭代器样式的方法。这将使您不必担心函数中的数组有多大:

void FillThisArray(CString* begin, CString* end)
{
    for (CString* iter = begin; iter != end; ++iter)
    {
        *iter = "Some text";
    }
}

int main()
{
    CString* pstrArray = new CString[nMySize];
    FillThisArray(&pstrArray[0], &pstrArray[nMySize]);

    for (int i = 0; i < nMySize; ++i)
    {
        assert(pstrArray[i] == "Some_text");
    }

    delete[] pstrArray;
}

您甚至可以模板化您的函数,以便它不与 pstrArray 的(有问题的)实现相关联:

template <typename T>
void FillThisArray(T begin, T end)
{
    for (T iter = begin; iter != end; ++iter)
    {
        *iter = "Some text";
    }
}

int main()
{
    {
        CString* pstrArray = new CString[nMySize];
        FillThisArray(&pstrArray[0], &pstrArray[nMySize]);

        for (int i = 0; i < nMySize; ++i)
        {
            assert(pstrArray[i] == "Some text");
        }

        delete[] pstrArray;
    }
    {
        std::vector<std::string> better(nMySize);
        FillThisArray(better.begin(), better.end());
        for (int i = 0; i < nMySize; ++i)
        {
            assert(better[i] == "Some text");
        }
    }
}

If there's a very good reason why you cannot use a standard container class, consider taking an iterator-style approach. This would save you having to worry about how big the array is in your function:

void FillThisArray(CString* begin, CString* end)
{
    for (CString* iter = begin; iter != end; ++iter)
    {
        *iter = "Some text";
    }
}

int main()
{
    CString* pstrArray = new CString[nMySize];
    FillThisArray(&pstrArray[0], &pstrArray[nMySize]);

    for (int i = 0; i < nMySize; ++i)
    {
        assert(pstrArray[i] == "Some_text");
    }

    delete[] pstrArray;
}

You could even template your function so that it is not tied to the (questionable) implementation of pstrArray:

template <typename T>
void FillThisArray(T begin, T end)
{
    for (T iter = begin; iter != end; ++iter)
    {
        *iter = "Some text";
    }
}

int main()
{
    {
        CString* pstrArray = new CString[nMySize];
        FillThisArray(&pstrArray[0], &pstrArray[nMySize]);

        for (int i = 0; i < nMySize; ++i)
        {
            assert(pstrArray[i] == "Some text");
        }

        delete[] pstrArray;
    }
    {
        std::vector<std::string> better(nMySize);
        FillThisArray(better.begin(), better.end());
        for (int i = 0; i < nMySize; ++i)
        {
            assert(better[i] == "Some text");
        }
    }
}
明媚如初 2024-10-11 03:35:53

您需要将指针传递给第一个元素以及可用元素的数量:

void FillThisArray(CString* strings, size_t num)
{
}

You need to pass the pointer to the first element, and the number of available elements:

void FillThisArray(CString* strings, size_t num)
{
}
烟雨扶苏 2024-10-11 03:35:53

您必须将动态分配的数组作为指向其第一个元素的指针传递。有两种语法,两者都是等效的:

void FillThisArray(CString* strArray)
{
}

或者

void FillThisArray(CString  strArray[])
{
}

您可以在函数内使用 strArray 参数作为数组。请注意,指针不保存有关数组实际大小的信息,因此如果该大小不是全局可用的,您应该将该大小作为第二个参数传递。哈

You must pass a dymanically allocated array as a pointer to its first element. There are two syntaxes for that, both are equivalent:

void FillThisArray(CString* strArray)
{
}

or

void FillThisArray(CString  strArray[])
{
}

you can use the strArray parameter as an array inside the function. Please note that the pointer doesn't hold information about your array's actual size, so if the size is not globally available you should pass the size as the second parameter. hth

笑忘罢 2024-10-11 03:35:53
void FillThisArray(CString* pstrArray)

你不能这样做吗?

void FillThisArray(CString* pstrArray)

Can't u do this?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文