📜  C++中的boost :: type_traits :: is_array模板

📅  最后修改于: 2021-05-30 17:41:13             🧑  作者: Mango

Boost C++库的std :: boost :: is_array模板用于检查给定类型是否为数组类型。它返回一个显示相同值的布尔值。

头文件:

#include "boost/type_traits/is_array.hpp" 
or 
#include "boost/type_traits.hpp"

模板类别:

template 
struct is_array : public true_type-or-false_type {};

如果T是数组类型,则从true_type继承,否则从false_type继承。

句法:

boost::integral_constant ::value
boost::integral_constant ::value_type
boost::integral_constant ::type

参数:此模板接受单个参数T(特质类),以检查T是否为指针类型。

接受的参数:

typename T
volatile T []
const volatile T []
const T []
T []
const volatile T[N]
volatile T [N]
const T [N]
T [N]

返回值:该模板返回一个布尔值,如下所示:

  • True:如果类型是指针值。
  • False:如果类型是非指针值。

下面的程序说明了std :: boost :: type_traits :: is_array模板:
程序1:

// C++ program to illustrate std::is_array template
#include 
#include 
#include 
using namespace std;
  
// Main Program
int main()
{
    cout << "is_array: \n";
    cout << "int[]: "
         << boost::is_array::value
         << "\n";
    cout << "char[]: "
         << "boost::is_array::value"
         << "\n";
  
    cout << "double[20]: "
         << boost::is_array::value
         << "\n";
    cout << "float[30]: "
         << boost::is_array::value
         << "\n";
  
    cout << "bool[][6]: "
         << boost::is_array::value
         << "\n";
    cout << "long[56][34][98]: "
         << boost::is_array::value
         << "\n";
  
    bool a[98];
  
    cout << "bool a[98]: "
         << boost::is_array::value
         << "\n";
  
    char c[0];
  
    cout << "char c[0]: "
         << boost::is_array::value
         << "\n";
  
    return 0;
}
输出:
is_array: 
int[]: 1
char[]: boost::is_array::value
double[20]: 1
float[30]: 1
bool[][6]: 1
long[56][34][98]: 1
bool a[98]: 1
char c[0]: 0

程式2:

// C++ program to illustrate std::is_array template
#include 
#include 
#include 
using namespace std;
  
// A Structure
struct sturec {
    int x, y;
    float a, b;
    long t[90];
};
  
// A Class
class student {
private:
    string name;
    int roll_no;
  
public:
    student(string name, int roll_no);
    string studentName(int roll_no);
};
  
// Parameterized Constructor
student::student(string name, int roll_no)
{
    this->name = name;
    this->roll_no = roll_no;
}
  
// Function that return the name
// of the student
string student::studentName(int roll_no)
{
    return this->name;
}
  
// Main Program
int main()
{
    cout << "is_array: \n";
  
    sturec s;
    sturec p[3];
  
    cout << "instance of structure: "
         << boost::is_array::value
         << "\n";
    cout << "array of structure: "
         << boost::is_array::value
         << "\n";
  
    // Instance of Class
    student S("GeeksforGeeks", 11840520);
    cout << "instance of a class: "
         << boost::is_array::value
         << "\n";
  
    // Array of Class
    student a[3] = {
        { "Abc Def", 11840820 },
        { "Xyz Xyz", 11840220 },
        { "ABcd Gfgh", 11840950 }
    };
  
    cout << "array of a class: "
         << boost::is_array::value
         << "\n";
  
    return 0;
}
输出:
is_array: 
instance of structure: 0
array of structure: 1
instance of a class: 0
array of a class: 1
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”