[C++] Notes (2)

1. C++虽然支持C风格的字符串,但是,因为这种字符串容易出错以及导致安全问题,C++ 程序不应该使用。strcat, strcpy,strncat,strncpy 等,用户必须自行计算分配存储空间长度,使用十分麻烦,相比之下,用C++ string,code简短易读不易出错。

2. string literal 是C++ 从 C 继承而来的 C 风格的字符串,实质为 const char 数组,C风格字符串本质为以NULL 终止的字符数组。

char ca1[] = {'C', '+', '+'};        // no null, not C-style string
char ca2[] = {'C', '+', '+', '\0'};  // explicit null
char ca3[] = "C++";     // null terminator added automatically
const char *cp = "C++"; // null terminator added automatically
char *cp1 = ca1;   // points to first element of a array, but not C-style string
char *cp2 = ca2;   // points to first element of a null-terminated char array

Library string 可以用关系操作符比较大小,但是C-Style string 不能用关系操作符,因为比较的会实际上是地址。如果要使用C字符串库函数,那么字符串务必要以0终止。

3. <bitset> 用于进行位操作,可用字符串初始化bitset,但是必须是二进位,长度如果大于size则截断,小于size则高阶位补0。

bitset<64> bitvec(32);
cout << bitvec << endl;  //  0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0010 0000
bitset<32> bv1(1010101);
cout << bv1 << endl;  // 0000 0000 0000 0000 0000 0000 0101 0101

string bstr; 
cin >> bstr; 
bitset<8> bv2(bstr);  
cout << bv2 << endl;  // input:100000, output:0010 0000

vector对象用类型相区别,但是bitset用size相区别,bitset构造函数:

bitset<n> b;

b has n bits, each bit is 0

bitset<n> b(u);

b is a copy of the unsigned long value u

bitset<n> b(s);

b is a copy of the bits contained in string s

bitset<n> b(s, pos, n);

b is a copy of the bits in n characters from s starting from position pos

如果用string 初始化bitset,bit从右到左读取:

string strval("1100");
bitset<8> bitvec4(strval);  // 00001100
bitset一系列操作:

b.any()

Is any bit in b on?

b.none()

Are no bits in b on?

b.count()

Number of bits in b that are on

b.size()

Number of bits in b

b[pos]

Access bit in b at position pos

b.test(pos)

Is bit in b in position pos on?

b.set()

Turn on all bits in b

b.set(pos)

Turn on the bit in b at position pos

b.reset()

Turn off all bits in b

b.reset(pos)

Turn off the bit in b at position pos

b.flip()

Change the state of each bit in b

b.flip(pos)

Reverse value of the bit in b in position pos

b.to_ulong()

Returns an unsigned long with the same bits as in b

os << b

Prints the bits in b to the stream os

on 为 1, off 为 0, flip表示反转,b.size(),b.count() 返回类型为 size_t. 这一类型足够大可以保持对象的size.

string str("1111111000000011001101");
bitset<8> bitvec5(str, 5, 4); //4 bits starting at str[5], 1100, bitset 表示 00001100, 最左边bitset[7], 最右边bitset[0] 
bitset<32> bitvec6(str, str.size() - 4); // use last 4 characters

bitset 用图形表示的话,左边表示高阶位,右边表示低阶位。

bitset<8> b("00100000");
cout << b.to_ullong() << endl;   // 输出32

bitset可以用来进行二进制数的一系列操作: | & ^ ~ >> <<

4. C++ string 和 C-style string 的相互转换:

可以用字符串字面值初始化string:

string st3("Hello World");  // st3 holds Hello World

因为C-style string和字符串字面值类型相同,可以用字符串字面值的地方,同样也可以用C-style string:

string::c_str 返回 const char*,  假定 st2 为字符串,下面表达式错误:

char *str = st2.c_str(); // error!!

必须改为: const char* str = st2.c_str() 或  char* str = const_cast<char*>(st2.c_str())

5.  动态分配数组时对值初始化:

int *pia2 = new int[10] (); 

以上10 个 int 值都初始化为0,动态分配的数组值只能初始化为元素类型的默认值,不同于数组变量元素。

如果是类对象,则调用该类的默认构造函数:

string *psa = new string[10]; // array of 10 empty strings
int *pia = new int[10];       // array of 10 uninitialized ints
6. 默认构造函数 ->无参构造函数。用户可以自己写一个不带参数的默认构造函数。如果类没有构造函数,编译器会自动提供一个合成的默认构造函数。如果类有定义其他带参数的构造函数,编译器不会再提供默认构造函数,如果要使用,用户自己定义。



猜你喜欢

转载自blog.csdn.net/ftell/article/details/80074330