视觉C++数组文本
Visual C++ Array Literals
我在Windows中工作,我想通过netbeans创建我的程序,所以我需要通过cygwin使用Linux编译器。到目前为止,我已经能够像这样使用int
的文字数组:
int x[6] = {1, 2, 3, 4, 5, 6}
或在方法中
methodName({1, 2, 3, 4, 5, 6})
我最近改用了Visual Studio,但我不被允许这样做。有没有其他选择?
int x[6] = {1, 2, 3, 4, 5, 6}
这种语法已经被MSVC2010所支持
。methodName({1, 2, 3, 4, 5, 6})
但这不受支持。如上所述,它只对"int const (&)[6]"函数参数有意义。
您可以使用以下语法:
methodName(make_array(1,2,3,4,5,6));
实现示例:
#include <iostream>
#include <ostream>
using namespace std;
template<typename T,unsigned size>
struct Array
{
typedef T type[size];
mutable type data;
Array()
{
cout << "Array::Array" << endl;
}
~Array()
{
cout << "Array::~Array" << endl;
}
};
template<typename T> inline
typename Array<T,1>::type &make_array(const T &p1,const Array<T,1> &aux=Array<T,1>())
{
aux.data[0]=p1;
return aux.data;
}
template<typename T> inline
typename Array<T,2>::type &make_array(const T &p1,const T &p2,const Array<T,2> &aux=Array<T,2>())
{
aux.data[0]=p1;
aux.data[1]=p2;
return aux.data;
}
template<typename T> inline
typename Array<T,3>::type &make_array(const T &p1,const T &p2,const T &p3,const Array<T,3> &aux=Array<T,3>())
{
aux.data[0]=p1;
aux.data[1]=p2;
aux.data[2]=p3;
return aux.data;
}
// ...
void test_array(int (&p)[3])
{
cout << p[0] << " " << p[1] << " " << p[2] << endl;
}
void test_ptr(int *p)
{
cout << p[0] << " " << p[1] << " " << p[2] << endl;
}
int main(int argc,char *argv[])
{
test_array(make_array(33,22,11));
test_ptr(make_array(33,22,11));
return 0;
}
或者,在 Boost.Preprocessor 的帮助下:
#include <iostream>
#include <ostream>
using namespace std;
// ______________________________________________________________
template<typename T,unsigned size>
struct Array
{
typedef T type[size];
mutable type values;
Array()
{
cout << "Array::Array" << endl;
}
~Array()
{
cout << "Array::~Array" << endl;
}
};
#include <boost/preprocessor/iteration/local.hpp>
#include <boost/preprocessor/repetition/enum.hpp>
#include <boost/preprocessor/repetition/repeat.hpp>
#define MAKE_ARRAY_PP_MAX_ARG_COUNT 16
#define MAKE_ARRAY_PP_PARAM_LIST(z, n, data) const T & BOOST_PP_CAT(p, n)
#define MAKE_ARRAY_PP_PARAM_ASSIGN(z, n, data) aux.values[n] = BOOST_PP_CAT(p, n);
#define BOOST_PP_LOCAL_MACRO(n)
template<typename T> inline
typename Array<T,n>::type &make_array(BOOST_PP_ENUM(n, MAKE_ARRAY_PP_PARAM_LIST, _) , const Array<T,n> &aux=Array<T,n>())
{
BOOST_PP_REPEAT(n, MAKE_ARRAY_PP_PARAM_ASSIGN, _)
return aux.values;
}
/**/
#define BOOST_PP_LOCAL_LIMITS (1, MAKE_ARRAY_PP_MAX_ARG_COUNT)
#include BOOST_PP_LOCAL_ITERATE()
// ______________________________________________________________
void test_array(int (&p)[3])
{
cout << p[0] << " " << p[1] << " " << p[2] << endl;
}
void test_ptr(int *p)
{
cout << p[0] << " " << p[1] << " " << p[2] << endl;
}
int main(int argc,char *argv[])
{
test_array(make_array(33,22,11));
test_ptr(make_array(33,22,11));
make_array(33,22,11,00,55,44,66);
make_array(1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16);
return 0;
}
相关文章:
- 2D数组来自文本输入,中间有空格
- 如何将内容数组写入文本文件?
- C++将文本文件中的数据读取到结构数组中
- 将值从二维数组输出到文本文件
- C++ 使用存储在动态数组中的文本文件中的数据查找模式
- C++编程从外部文本文件定义数组大小
- 使用矢量将文本文件中的输入存储到 2D 数组中
- 如何将文本文件的特定行读取到 int 类型的数组中C++?
- 通过分隔符分隔包含 UTF-16 BE 文本的uint8_t数组
- 如何检测将文本文件读入 int 数组的新行
- C++快速将 int 数组内容转储到文本文件中
- 如何将文本文件读取到二维数组中并以 c++ 打印
- 无法将整个文本文件复制到字符数组
- 如何在 c++ 中将文本文件传递到 2D 数组中?
- 是否可以创建一个用户定义的文本,将字符串文本转换为 own 类型的数组?
- 使用空格分隔符从文本文件将对象读入数组
- 为什么多维数组中的空字符串文本衰减为空指针?
- 从文本文件中读取并输入到数组结构中,然后显示读取的数据C++
- 我无法将数据从数据网格列传输到 C# 中的数组以获取文本框建议收集数据
- 如何在给定文本中用字符数组替换符号