while(数组末尾)-如何识别

发布于 2024-11-26 17:00:35 字数 609 浏览 2 评论 0原文

我有一个数字数组 {1,2,3,4,5} 或一个字符数组或其他。我想编写一个模板方法来打印完整的数组。它有效,只是存在一些问题。也许我先发布代码:

template <typename A>
void printArray(A start) {
    int i = 0;
    while (start[i] != 0) {
        std::cout << start[i] << std::endl;
        i++;
    }
}

int main(int argc, char **argv) {

    using namespace std;
    int xs[] = {1,2,3,4,5,6,7}; //works
    //int xs[] = {1,0,3,6,7}; of course its not working (because of the 0)
    int *start = xs;

    printArray(start);

    return 0;
}

你能看到问题吗? while(start[i] != 0) 不是读取数组结束的最佳方式;)我还有什么其他选择?

谢谢你!

I have an array of numbers {1,2,3,4,5} or an array of chars or whatever. I want to a write a template method to print out the full array. It works, there are just some problems. Maybe i post first the code:

template <typename A>
void printArray(A start) {
    int i = 0;
    while (start[i] != 0) {
        std::cout << start[i] << std::endl;
        i++;
    }
}

int main(int argc, char **argv) {

    using namespace std;
    int xs[] = {1,2,3,4,5,6,7}; //works
    //int xs[] = {1,0,3,6,7}; of course its not working (because of the 0)
    int *start = xs;

    printArray(start);

    return 0;
}

Can you see the problem? while(start[i] != 0) is not the best way to read an array to end ;) What do I have for other options?

Thank you!

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

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

发布评论

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

评论(5

终止放荡 2024-12-03 17:00:35

选项 1: 传递指针和元素数量

 template<class T>
 void doSth(T* arr, int size)

Upside - 适用于动态数组和自动数组。
缺点 - 您必须知道尺寸。你必须通过它。

选项2:使用自动推导出的大小参数化模板

template <class T, int N>
void doSth(T(&arr)[N])

缺点 - 动态数组无法传递

选项3 : 成为一名优秀的程序员并使用 std::vector

Option 1: pass a pointer and the number of elements

 template<class T>
 void doSth(T* arr, int size)

Upside - will work with both dynamic and automatic arrays.

Downside - you must know the size. You must pass it.

Option2: parametrize the template with the size which will be auto-deduced

template <class T, int N>
void doSth(T(&arr)[N])

Downside - Dynamic arrays cannot be passed

Option 3: Be a good programmer and use std::vector's

長街聽風 2024-12-03 17:00:35

由于您使用的是 C++,因此从长远来看,vectoriterators 会给您带来更好的效果。

Since you are using C++, a vector<int> and iterators will do you better over the long haul.

若言繁花未落 2024-12-03 17:00:35

如果您想使用数组而不是指针,那么您可以将函数模板编写为:

template <typename T, size_t N>
void printArray(T (&start)[N]) 
{
    int i = 0;
    while ( i < N) {
        std::cout << start[i] << std::endl;
        i++;
    }
}

int xs1[] = {1,2,3,4,5,6,7}; 
int xs2[] = {1,0,3,6,7}; 
printArray(xs1); //okay
printArray(xs2); //okay

int *start = xs1;
printArray(start); //error - cannot pass pointer anymore!

因此更好的解决方案是:std::vector

或者甚至更好地使用范围(迭代器对),这是非常惯用的,因为:

template <typename FwdIterator>
void printArray(FwdIterator begin, FwdIterator end) 
{
    while (begin != end) {
        std::cout << *begin << std::endl;
        begin++;
    }
}

int xs1[] = {1,2,3,4,5,6,7}; 
int xs2[] = {1,0,3,6,7}; 
printArray(xs1, xs1 + sizeof(xs1)/sizeof(xs1[0])); //okay
printArray(xs2, xs2 + sizeof(xs2)/sizeof(xs2[0])); //okay

int *start = xs1;
printArray(start, start + sizeof(xs1)/sizeof(xs1[0])); //okay!

printArray现在可以与std::vector还有:

std::vector<int> vec; //you can use std::list as well!
//populate vec

printArray(vec.begin(), vec.end());

If you want to use array, not pointer, then you can write the function template as:

template <typename T, size_t N>
void printArray(T (&start)[N]) 
{
    int i = 0;
    while ( i < N) {
        std::cout << start[i] << std::endl;
        i++;
    }
}

int xs1[] = {1,2,3,4,5,6,7}; 
int xs2[] = {1,0,3,6,7}; 
printArray(xs1); //okay
printArray(xs2); //okay

int *start = xs1;
printArray(start); //error - cannot pass pointer anymore!

So better solution would be : std::vector<T>.

Or even better use range (pair of iterators) which is very idiomatic, as:

template <typename FwdIterator>
void printArray(FwdIterator begin, FwdIterator end) 
{
    while (begin != end) {
        std::cout << *begin << std::endl;
        begin++;
    }
}

int xs1[] = {1,2,3,4,5,6,7}; 
int xs2[] = {1,0,3,6,7}; 
printArray(xs1, xs1 + sizeof(xs1)/sizeof(xs1[0])); //okay
printArray(xs2, xs2 + sizeof(xs2)/sizeof(xs2[0])); //okay

int *start = xs1;
printArray(start, start + sizeof(xs1)/sizeof(xs1[0])); //okay!

printArray now can be use with std::vector<T> also:

std::vector<int> vec; //you can use std::list as well!
//populate vec

printArray(vec.begin(), vec.end());
不疑不惑不回忆 2024-12-03 17:00:35

传递数组的地址及其长度。

Pass both the address of array and its length.

只涨不跌 2024-12-03 17:00:35

如果您使用的是编译时数组,则可以使用模板来执行此操作:

template <typename T, size_t N>
static inline void print_array(const T (&a)[N])
{
  for (size_t i = 0; i < N; ++i)
    std::cout << a[i] << std::endl;
}

int main()
{
  int a[] = {1,2};
  print_array(a);
}

如果您需要的只是打印数组,还请查看漂亮的打印机,它在内部使用这种东西。

If you are using a compile-time array, you can do this with a template:

template <typename T, size_t N>
static inline void print_array(const T (&a)[N])
{
  for (size_t i = 0; i < N; ++i)
    std::cout << a[i] << std::endl;
}

int main()
{
  int a[] = {1,2};
  print_array(a);
}

If all you need is to print arrays, also check out the pretty printer, which uses this sort of stuff internally.

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