C++ 向量中的成员类型是什么意思?
当我浏览at上的C++ 向量中的成员类型是什么意思?,c++,c++11,vector,stl,C++,C++11,Vector,Stl,当我浏览at上的std::vector,发现一个名为“成员类型”的部分时,我不明白这是什么意思。事实上,成员类型部分存在于stl库中容器的所有参考文档中 有人能帮我理解这个吗?这是一个典型的C++类: struct Foo { int n; // non-static data member static const float x; // static data member int f() const
std::vector
,发现一个名为“成员类型”的部分时,我不明白这是什么意思。事实上,成员类型部分存在于stl
库中容器的所有参考文档中
有人能帮我理解这个吗?这是一个典型的C++类:
struct Foo
{
int n; // non-static data member
static const float x; // static data member
int f() const; // non-static member function
static int g(); // static member function
template <typename T>
void h(T); // non-static member function template
struct X { bool a; }; // member type
template <typename> struct Q; // member class template
using T = Q<int>; // member type (member typedef)
};
structfoo
{
int n;//非静态数据成员
static const float x;//静态数据成员
int f()const;//非静态成员函数
static int g();//静态成员函数
样板
void h(T);//非静态成员函数模板
结构X{bool a;};//成员类型
模板结构Q;//成员类模板
使用T=Q;//成员类型(成员类型定义)
};
用法示例:
// Use static members
print(Foo::x);
print(Foo::g());
Foo::X y;
Foo::Q<double> z;
Foo::T w;
// Use non-static members (requires instance)
void demo(const Foo & a)
{
print(a.n);
print(a.f());
print(a.h<float>());
}
//使用静态成员
打印(Foo::x);
打印(Foo::g());
Foo:xy;
Foo::qz;
Foo::tw;
//使用非静态成员(需要实例)
void演示(const Foo&a)
{
印刷品(a.n);
打印(a.f());
打印(a.h());
}
这是C++中的一个典型类:
struct Foo
{
int n; // non-static data member
static const float x; // static data member
int f() const; // non-static member function
static int g(); // static member function
template <typename T>
void h(T); // non-static member function template
struct X { bool a; }; // member type
template <typename> struct Q; // member class template
using T = Q<int>; // member type (member typedef)
};
structfoo
{
int n;//非静态数据成员
static const float x;//静态数据成员
int f()const;//非静态成员函数
static int g();//静态成员函数
样板
void h(T);//非静态成员函数模板
结构X{bool a;};//成员类型
模板结构Q;//成员类模板
使用T=Q;//成员类型(成员类型定义)
};
用法示例:
// Use static members
print(Foo::x);
print(Foo::g());
Foo::X y;
Foo::Q<double> z;
Foo::T w;
// Use non-static members (requires instance)
void demo(const Foo & a)
{
print(a.n);
print(a.f());
print(a.h<float>());
}
//使用静态成员
打印(Foo::x);
打印(Foo::g());
Foo:xy;
Foo::qz;
Foo::tw;
//使用非静态成员(需要实例)
void演示(const Foo&a)
{
印刷品(a.n);
打印(a.f());
打印(a.h());
}
您链接的页面列出了“值类型T”的“成员类型”。这是属于或是T的成员的类型定义。请考虑
using VEC = std::vector<double>;
VEC dv { 4.2 };
或者,如果您想编写跨平台的可移植代码,使用“大小\类型”来确保存储足够大的大小可能会有所帮助:
int s = dv.size(); // wrong on 64-bit system
VEC::size_type s = dv.size(); // always correct
也就是说,std::vector
承载一个类型定义,value\u type
,其计算结果为vector中的'T'类型,或者在我们的例子中为double
这对于泛型编程非常重要:
template<typename T>
void dothing(const T& container)
{
T::const_pointer* c = &container; // for some reason I need it's address
// .. other stuff
}
模板
空心圆点(常数T和容器)
{
T::const_pointer*c=&container;//出于某种原因,我需要它的地址
//…其他东西
}
您链接的页面列出了“值类型T”的“成员类型”。这是属于或是T的成员的类型定义。请考虑
using VEC = std::vector<double>;
VEC dv { 4.2 };
或者,如果您想编写跨平台的可移植代码,使用“大小\类型”来确保存储足够大的大小可能会有所帮助:
int s = dv.size(); // wrong on 64-bit system
VEC::size_type s = dv.size(); // always correct
也就是说,std::vector
承载一个类型定义,value\u type
,其计算结果为vector中的'T'类型,或者在我们的例子中为double
这对于泛型编程非常重要:
template<typename T>
void dothing(const T& container)
{
T::const_pointer* c = &container; // for some reason I need it's address
// .. other stuff
}
模板
空心圆点(常数T和容器)
{
T::const_pointer*c=&container;//出于某种原因,我需要它的地址
//…其他东西
}
您需要了解两件事:
using blah = int;
将使blah
成为int
的别名。换句话说,您可以在任何位置使用blah
而不是int
(还有另一种使用typedef
而不是使用创建类型别名的方法,但可读性较差。)
您可以将这些别名放入类中。
例如,如果您声明了以下结构
struct S
{
using type = int;
};
您可以使用S::type
而不是int
类内部的此类别名通常称为成员类型。有两件事需要了解:
您可以为类型创建别名。
比如说,
using blah = int;
将使blah
成为int
的别名。换句话说,您可以在任何位置使用blah
而不是int
(还有另一种使用typedef
而不是使用创建类型别名的方法,但可读性较差。)
您可以将这些别名放入类中。
例如,如果您声明了以下结构
struct S
{
using type = int;
};
您可以使用S::type
而不是int
类内部的此类别名通常称为成员类型。成员类型定义向量对象使用的类型。许多标准容器使用成员类型来描述所使用的类型,因此程序员不需要手动计算它们。这在处理复杂模板时特别有用,因为在复杂模板中,类型可能很难确定
例如,返回类型通常是,但是另一个C++向量实现可以返回不同的整数类型(例如)。您可以简单地使用向量公开的成员类型来编写代码,每次都使用完美的类型,而不是假设代码中需要什么类型(并可能引入危险的强制转换)。例如:
std::vector<int> vec;
const std::vector<int>::value_type val = 4;
// std::vector<int>::value_type is a typedef of int!
vec.push_back(val);
const std::vector<int>::size_type size = vec.size();
// std::vector<int>::size_type is a typedef of std::size_t, usually.
const std::size_t size2 = vec.size();
// same as above, but we assume that vec.size() returns a size_t.
// If it does not, we may cause a narrowing conversion!
for (std::vector<int>::const_iterator it = vec.begin(), end = vec.end(); it != end; ++it)
{
// The const_iterator type is also a member type of vector.
std::cout << *it << std::endl;
}
我们现在可以做:
const auto size = vec.size();
编译器会自动计算出来
一般来说,大多数C++标准对象在可能的情况下都会使用相同的成员类型(例如<代码> SigZeT<<代码> >代码> SigZyType < /代码>,<代码> t>代码> <代码> ValueSype类型,<代码> T>和<代码> <代码>参考类型>代码>,但不能保证。(iterator
对于std::vector
和std::list
,成员类型是不同的,因为它们的实现非常不同,并且不能使用相同的迭代器类型)。成员类型是d