标签归档:C++

字符串排序函数(单词排序,唯一)

本程序演示:怎样将一个string字符串输入到一个vector中,每个元素对应一个单词。

// sort and unique
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using std::cin;
using std::cout;
using std::ends;
using std::endl;
using std::string;
using std::vector;
using std::sort;

void show(vector<string> const &words);
void elimDups(vector<string> &words);
int main(void)
{
        string str("the quick red fox jumps over the slow red turtle");
        //string str("");

        string separators = " \t:;,\v\n\r\f";
        string word;
        vector<string> vec;
        std::string::size_type startPos = 0, endPos = 0;
        std::string::size_type /*count = 0,*/ wordLen = 0;
        while ((startPos = str.find_first_not_of(separators, endPos))
                != std::string::npos) {
//              ++count;
                endPos = str.find_first_of(separators, startPos);
                if (endPos == std::string::npos) {
                        wordLen = str.size() - startPos;
                } else {
                        wordLen = endPos -startPos;
                }
                word.assign(str.begin() + startPos,
                        str.begin() + startPos + wordLen);
                vec.push_back(word);
        }
        // show vec info
        show(vec);

        elimDups(vec);

        return 0;
}

void elimDups(vector<string> &words)
{
        sort(words.begin(), words.end());
        show(words);
        auto end_unique = unique(words.begin(), words.end());
        show(words);
        words.erase(end_unique, words.end());
        show(words);
}

void show(vector<string> const &words)
{
        for (auto s : words)
                cout << s << " ";
        cout << endl;

        return;
}


程序输出:

[lhf@localhost study]$ ./23
the quick red fox jumps over the slow red turtle
fox jumps over quick red red slow the the turtle
fox jumps over quick red slow the turtle the red
fox jumps over quick red slow the turtle

c++primer 9.51习题

设计一个类,它有三个unsigned成员,分别表示年月日。为其编写构造函数,接受一个表示日期的string参数。你的构造函数能够处理不同的数据格式,如January 1, 1900 1/1/1900 Jan 1 1900等

// January 1, 1900      1/1/1900        Jan 1 1900
#include <iostream>
#include <vector>
#include <string>
using std::cin;
using std::cout;
using std::endl;
using std::ends;
using std::vector;
using std::string;
using std::stoi;

class Date {
public:
        vector<string> full_month{
                "January", "February", "March", "April", "May", "June", "July",
                "August", "September", "October", "November", "December"
        };
        vector<string> abb_month{
                "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept",
                "Sep", "Oct", "Nov", "Dec"
        };
public:
        Date(const string& info);
        ~Date(void) {}
        void print_info(void)
        {
                cout << "_" << month << "." << day << "."<< year << "_\n";
                return;
        }
private:
        void init(void)
        {
                year = 1900;
                month = 1;
                day = 1;

                return;
        }
private:
        unsigned year;
        unsigned month;
        unsigned day;
};

int main(void)
{

        cout << "2343" << endl;
        Date error("2343");
        error.print_info();
        cout << string(20, '*') << endl;

        cout << "October 21, 2002" << endl;
        Date date1("October 21, 2002");
        date1.print_info();
        cout << string(20, '*') << endl;

        cout << "2/8/1995" << endl;
        Date date2("2/8/1995");
        date2.print_info();
        cout << string(20, '*') << endl;

        cout << "May 12 2011" << endl;
        Date date3("May 12 2011");
        date3.print_info();

        return 0;
}

Date::Date(const string& info)
{
        std::string::size_type pos;
        pos = info.find_first_of(", /");

        // error info
        if (pos == std::string::npos) {
                init();
                cout << "error date format: init" << endl;
                return;
        }

        // 1/1/1900
        std::string::size_type pos1 = info.find_first_of('/'),
                pos11 = pos1;
        if (pos1 != std::string::npos) {
                day = stoi(info.substr(0, pos1));
                pos11 = info.find_first_of('/', pos1 + 1);
                month = stoi(info.substr(pos1 + 1, pos11));
                year = stoi(info.substr(pos11 + 1, info.size() - 1));
                return;
        }

        // January 1, 1990 and Jan 1 1900
        std::string::size_type pos2 = info.find_first_of(' '),
                pos3 = pos2, pos33 = pos3, pos22 = info.find_first_of(',');
        if (pos22 != std::string::npos) {
                // January 1, 1990
                string str_month = info.substr(0, pos2);
                vector<string>::size_type n = 0;
                for (; n != full_month.size(); ++n) {
                        if (!full_month[n].compare(str_month))
                                break;
                }
                if (n == full_month.size()) {
                        init();
                        cout << "format(January 1, 1990) error: init" << endl;
                        return;
                } else {
                        month = n + 1;
                }
                day = stoi(info.substr(pos2 + 1, pos22));
                year = stoi(info.substr(pos22 + 1, info.size() -1));

                return;
        } else if (info.find_first_of(' ', pos3 + 1) != std::string::npos) {
                // Jan 1 1990
                vector<string>::size_type n = 0;
                string str_month = info.substr(0, pos3);
                for (; n != abb_month.size(); ++n) {
                        if (!abb_month[n].compare(str_month))
                                break;
                }
                if (n == abb_month.size()) {
                        init();
                        cout << "format(Jan 1 1990) error: init" << endl;
                        return;
                } else if (n < 9) {
                        month = n + 1;
                } else if (n == 9) {
                        month = n;
                } else {
                        month = n - 1;
                }
                pos33 = info.find_first_of(' ', pos3 + 1);
                day = stoi(info.substr(pos3 + 1, pos33));
                year = stoi(info.substr(pos33 + 1, info.size() - 1));
        } else {
                init();
                cout << "other error format: init" << endl;

                return;
        }

        return;
}

测试运行结果:

$ ./my_date
2343
error date format: init
_1.1.1900_
********************
October 21, 2002
_10.21.2002_
********************
2/8/1995
_8.2.1995_
********************
May 12 2011
_5.12.2011_

C++编程语言中的所有运算符的优先级和结合性列表

C++运算符优先级和结核性列表

优先级 运算符 叙述 示例 重载性 结合性
1 :: 作用域解析(C++专有) Class::age = 2; 由左至右
2 ++ 后缀递增 i++
后缀递减 i–
{} 组合 {i++;a*=i;}
() 函数调用或变量初始化 c_tor(int x, int y) : _x(x), _y(y * 10) {}
[] 数组访问 array[4] = 2;
. 以对象方式访问成员 obj.age = 34;
-> 以指针方式访问成员 ptr->age = 34;
dynamic_cast 运行时检查类型转换(C++专有) Y& y = dynamic_cast<Y&>(x);
static_cast 未经检查的类型转换(C++专有) Y& y = static_cast<Y&>(x);
reinterpret_cast 重定义类型转换(C++专有) int const* p = reinterpret_cast<int const*>(0x1234);
const_cast 更改非常量属性(C++专有) int* q = const_cast<int*>(p);
typeid 获取类型信息(C++专有) std::type_info const& t = typeid(x);
3 ++ 前缀递增 ++i 由右至左
前缀递减 –i
+ 一元正号 int i = +1;
一元负号 int i = -1;
!
not
逻辑非
!的备用拼写
if (!done) …
~
compl
按位取反
~的备用拼写
flag1 = ~flag2;
(type) 强制类型转换 int i = (int)floatNum;
* 取指针指向的值 int data = *intPtr;
& 取变量的地址 int *intPtr = &data;
sizeof 某某的大小 size_t s = sizeof(int);
new 动态内存分配(C++专有) long* pVar = new long;
new[] 动态数组内存分配(C++专有) long* array = new long[20];
delete 动态内存释放(C++专有) delete pVar;
delete[] 动态数组内存释放(C++专有) delete [] array;
4 .* 成员对象选择(C++专有) obj.*var = 24; 由左至右
->* 成员指针选择(C++专有) ptr->*var = 24;
5 * 乘法 int i = 2 * 4;
/ 除法 float f = 10.0 / 3.0;
% 模数(取余) int rem = 4 % 3;
6 + 加法 int i = 2 + 3;
减法 int i = 5 – 1;
7 << 比特左移 int flags = 33 << 1;
>> 比特右移 int flags = 33 >> 1;
8 < 小于关系 if (i < 42) …
<= 小于等于关系 if (i <= 42) …
> 大于关系 if (i > 42) …
>= 大于等于关系 if (i >= 42) …
9 ==
eq
等于关系
==的备用拼写
if (i == 42) …
!=
not_eq
不等于关系
!=的备用拼写
if (i != 42) …
10 &
bitand
比特 AND
&的备用拼写
flag1 = flag2 & 42;
11 ^
xor
比特 XOR(独占or)
^的备用拼写
flag1 = flag2 ^ 42;
12 |
bitor
比特 OR(包含or)
|的备用拼写
flag1 = flag2 | 42;
13 &&
and
逻辑 AND
&&的备用拼写
if (conditionA && conditionB) …
14 ||
or
逻辑 OR
||的备用拼写
if (conditionA || conditionB) …
15 c?t:f 三元条件运算 int i = a > b ? a : b; 由右至左
16 = 直接赋值 int a = b;
+= 以和赋值 a += 3;
-= 以差赋值 b -= 4;
*= 以乘赋值 a *= 5;
/= 以除赋值 a /= 2;
%= 以取余数赋值 a %= 3;
<<= 以比特左移赋值 flags <<= 2;
>>= 以比特右移赋值 flags >>= 2;
&=
and_eq
以比特AND赋值
&=的备用拼写
flags &= new_flags;
^=
xor_eq
以比特XOR赋值
^=的备用拼写
flags ^= new_flags;
|=
or_eq
以比特OR赋值
|=的备用拼写
flags |= new_flags;
17 throw 抛出异常 throw EClass(“Message”);
18 , 逗号运算符 for (i = 0, j = 0; i < 10; i++, j++) … 由左至右