变量
1> 左值: 可以出现在赋值语句的左边或者右边。
2> 右值: 只能出现在赋值语句的右边。
标准库类型
意义: 语言组成部分中更基本的哪些数据类型的抽象
string
构造函数:
string s1; // 默认构造函数,空串
string s2(s1); // 由字符串s1来初始化s2
string s3("Hello World"); // 将s3初始化为“Hello World”
string s4(n, 'c'); // 将s4初始化为n个'c'组成的字符串
string读写
string输入操作符规则:
- 忽略开头的所有空白符(空格、换行、制表)
- 读取字符直到遇到空白符终止
getline读取整行文本:
- 不忽略开头空白符
- 遇到换行终止
- 如果第一个字符即为换行,即读取到空串
string 操作
size:
返回的结果要使用string::siez_type 类型接收
empty
<, <=, >, >=
赋值=:
赋值操作有时会带来消耗,如下str1先释放”lx”内容,后分配str2的内存大小的空间,将str2字符串内容拷贝到str1的空间当中
string str1("lx");
string str2("huster");
string str1 = str2;
连接+:
混合连接操作时,+操作符的左右操作数,至少有一个是string类型
string str1 = "hello";
string str2 = str1 + " world"; // ok
string str3 = "hello" + " world"; // error
下标[]:
- 传入下标中变量类型要为string::size_type
- 下标返操作返回左值(即可以通过下标修改字符串数据)
string 对象中字符处理:
使用cctype借用c库来处理字符
函数 | 功能 |
---|---|
isalnum(c) | c为数字或字母,返回true |
… |
string source_code
vc10
vc中string的类型定义
typedef basic_string<char, char_traits<char>, allocator<char> >
string;
template<class _Elem,
class _Traits,
class _Alloc>
class basic_string
: public _String_alloc<_String_base_types<_Elem, _Alloc> >
allocator 内存分配
// TEMPLATE CLASS allocator
template<class _Ty>
class allocator
{ // generic allocator for objects of class _Ty
public:
static_assert(!is_const<_Ty>::value,
"The C++ Standard forbids containers of const elements "
"because allocator<const T> is ill-formed.");
typedef void _Not_user_specialized;
typedef _Ty value_type;
typedef value_type *pointer;
typedef const value_type *const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
typedef true_type propagate_on_container_move_assignment;
typedef true_type is_always_equal;
template<class _Other>
struct rebind
{ // convert this type to allocator<_Other>
typedef allocator<_Other> other;
};
pointer address(reference _Val) const _NOEXCEPT
{ // return address of mutable _Val
return (_STD addressof(_Val));
}
const_pointer address(const_reference _Val) const _NOEXCEPT
{ // return address of nonmutable _Val
return (_STD addressof(_Val));
}
void deallocate(pointer _Ptr, size_type _Count)
{ // deallocate object at _Ptr
_Deallocate(_Ptr, _Count, sizeof (_Ty));
}
_DECLSPEC_ALLOCATOR pointer allocate(size_type _Count)
{ // allocate array of _Count elements
return (static_cast<pointer>(_Allocate(_Count, sizeof (_Ty))));
}
_DECLSPEC_ALLOCATOR pointer allocate(size_type _Count, const void *)
{ // allocate array of _Count elements, ignore hint
return (allocate(_Count));
}
...
};
获取引用地址
template<class _Ty> inline
_Ty *_Addressof(_Ty& _Val, false_type) _NOEXCEPT
{ // return address of object _Val
return (reinterpret_cast<_Ty *>(
&const_cast<char&>(
reinterpret_cast<const volatile char&>(_Val))));
}
_String_base_types
template<class _Ty,
class _Alloc0>
struct _String_base_types
{ // types needed for a container base
typedef _Alloc0 _Alloc;
typedef _String_base_types<_Ty, _Alloc> _Myt;
typedef _Wrap_alloc<_Alloc> _Alty0;
typedef typename _Alty0::template rebind<_Ty>::other _Alty;
typedef typename _If<_Is_simple_alloc<_Alty>::value,
_Simple_types<typename _Alty::value_type>,
_String_iter_types<typename _Alty::value_type,
typename _Alty::size_type,
typename _Alty::difference_type,
typename _Alty::pointer,
typename _Alty::const_pointer,
typename _Alty::reference,
typename _Alty::const_reference> >::type
_Val_types;
};
static_cast
常用于类型间的显示转换
- C++ compiler already knows how to convert scaler types such as float to int. Use static_cast for them.
- In general, while converting type A to B, static_cast would call B’s constructor passing it A. If B doesn’t have such constructor then you get compile time error.
- Cast from A* to B* always succeeds if A and B are in inheritance hierarchy (or void) otherwise you get compile error.
- Gotcha: If you cast base pointer to derived pointer but if its not valid cast then you don’t get error. You get bad pointer and as soon as you try to access members of derived pointer, you get segfault.
- Same goes for A& to B&.
- Gotcha: Cast from Derived to Base or viceversa creates new copy! For people coming from C#/Java, many of above can be a huge surprise.
reinterpret_cast
操作层次较低,通常从字节模式重新解释,常用作指针的转换
- This basically says that take these bytes at this memory location and think of it as given object.
- For example, you can load 4 bytes of float to 4 bytes of int to see how bits in float looks like.
- Obviously, if data is not correct for the type, you may get segfault.
- There is no runtime overhead for this cast.
参考: When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used?