English 中文(简体)
我怎么做像这种事情呢?
原标题:How do I do something like this some_function({1,1,1,1})?
  • 时间:2012-01-13 17:54:55
  •  标签:
  • c++
  • arrays

请允许我指出,我有这样的原型:int func(int * a),并且接受一个阵列作为理由。

如果没有汇编者显示各地的错误,我怎么做呢?

最佳回答

你们都需要的是<条码><>。

#include <iostream>

static void f (int* a) {
  while (*a) std::cout << *a++ << "
" ;
  }

int main() {
  f ((int[]){1,2,3,4,0}) ;
  }

守则产出

1
2
3
4

It works in C too -- see this ideone link.

www.un.org/Depts/DGACM/index_spanish.htm 增补: 页: 1 关于这一建筑的合法性的新问题,以及Mat s的回答,如果你对此类事情有兴趣的话,值得阅读。 简言之,这似乎只在C99有效,但一些汇编者允许它作为所有C/C++变量的延伸。

问题回答

与此类似:

int func(int * a);

void somewhere_else()
{
    int arr[4] = { 1, 1, 1, 1 };
    func(arr);
}

承诺使用原始阵列,当然不会给它们留下任何转子。 Ew! 我们再说不了1975年。

#include <cstddef>
#include <iostream>
#include <vector>

void func(std::vector<int> const& v) {
   for (std::size_t i = 0; i < v.size(); i++)
      std::cout << v[i] << " ";
}

int main() {
   func({ 1, 2, 3, 4 });
}

// Output: "1 2 3 4 "

这就需要一名符合C++11某些特征的汇编员。 姓名为初步编制者名单。

您可使用std: originalizer_list:

int func(std::initializer_list<int> a) {
  // do something with a here
}

或者,你可以书写一个使用<代码>的包装材料:初始器_list。 (如果出于某种原因,你不能改变原来的职能):

int func_wrapper(std::initializer_list<int> a) {
  std::vector<int> b = a;
  func(b.data());
}

这样做的一个途径是

 #include <iostream>
 #include <stdio.h>


  void abc (int *a,int z)
  {
   int m= z/sizeof(*a);    
   for(int i=0;i<m;i++)
      {
        std::cout<<"values " <<*a<<"
";
        a++;
      }     
  }
 int main()
 {
   int ar[]={11,12,13,14,15,1166,17};
   std::cout << sizeof(ar)<<"size
";
   abc(ar,sizeof(ar));   
   getchar();
 }

here in this case you dont need to worry about size and all. In case of int ar[3]={1,2,3} that will give junk values if you try and search for NULL as the third place is occupied by element 3





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签