📜  C++中的std :: remove_const与示例

📅  最后修改于: 2021-05-30 12:24:21             🧑  作者: Mango

头文件中提供了C++ STL的std :: remove_const模板。 C++ STL的std :: remove_const模板用于获取类型T,而无需const限定。如果T没有const限定,则返回布尔值true,否则返回false。以下是相同的语法:

头文件:

#include

模板类别:

template 
struct remove_const;

句法:

std::remove_const::value

参数:此模板std :: remove_const接受单个参数T(Trait类),以检查T是否没有const限定值。

返回值:模板std :: remove_const返回一个布尔值:

  • True:如果T类型没有const限定。
  • False:如果T类型具有const限定符。

下面是在C++中演示std :: remove_const的程序:

程序:

// C++ program to illustrate
// std::remove_const
#include 
#include 
using namespace std;
  
// Driver Code
int main()
{
    // Declare variable of type
    // int, const int, const int*,
    // int * const and const int&
    typedef remove_const::type A;
    typedef remove_const::type B;
    typedef remove_const::type C;
    typedef remove_const::type D;
    typedef remove_const::type E;
  
    cout << std::boolalpha;
  
    // Checking const of the above
    // declared variables
    cout << "A is without const int? "
         << is_same::value
         << endl;
  
    cout << "B is without const int? "
         << is_same::value
         << endl;
  
    cout << "C is without const int? "
         << is_same::value
         << endl;
  
    cout << "D is without const int? "
         << is_same::value
         << endl;
  
    cout << "E is without const int? "
         << is_same::value
         << endl;
  
    return 0;
}
输出:
A is without const int? true
B is without const int? true
C is without const int? false
D is without const int? false
E is without const int? false

参考: http://www.cplusplus.com/reference/type_traits/remove_const/

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”