作者归档:lne

linux shell 返回值的含义

Linux 下,在terminal下,控制台下运行命令,所有的命令在结束时,都会返回一个数字值,这个值即为返回值,每个返回值都对应着一个错误号,根据错误号我们可以大致推断这个函数执行的情况。另外在terminal下,有一个特殊的环境变量 $?,保存着前一个程序的返回值,我们可以试试:

[root@localhost ~]# ls
anaconda-ks.cfg  Documents  initial-setup-ks.cfg  Pictures  Templates
Desktop          Downloads  Music                 Public    Videos
[root@localhost ~]# echo $?
0

上面通过echo观察执行ls的返回值,发现是0,说明该命令执行成功,也就是说,如果 $? 变量的值不是 0 的话,就表示上一个命令在执行的过程中出错了。我们可以试着 ls 一个不存在的文件:

[root@localhost ~]# ls a
ls: cannot access a: No such file or directory
[root@localhost ~]# echo $?
2
[root@localhost ~]#

错误对照表:

"OS error code 1: Operation not permitted" "OS error code 2: No such file or directory" "OS error code 3: No such process" "OS error code 4: Interrupted system call"
"OS error code   5:  Input/output error"
"OS error code   6:  No such device or address"
"OS error code   7:  Argument list too long"
"OS error code   8:  Exec format error"
"OS error code   9:  Bad file descriptor"
"OS error code  10:  No child processes"
"OS error code  11:  Resource temporarily unavailable"
"OS error code  12:  Cannot allocate memory"
"OS error code  13:  Permission denied"
"OS error code  14:  Bad address"
"OS error code  15:  Block device required"
"OS error code  16:  Device or resource busy"
"OS error code  17:  File exists"
"OS error code  18:  Invalid cross-device link"
"OS error code  19:  No such device"
"OS error code  20:  Not a directory"
"OS error code  21:  Is a directory"
"OS error code  22:  Invalid argument"
"OS error code  23:  Too many open files in system"
"OS error code  24:  Too many open files"
"OS error code  25:  Inappropriate ioctl for device"
"OS error code  26:  Text file busy"
"OS error code  27:  File too large"
"OS error code  28:  No space left on device"
"OS error code  29:  Illegal seek"
"OS error code  30:  Read-only file system"
"OS error code  31:  Too many links"
"OS error code  32:  Broken pipe"
"OS error code  33:  Numerical argument out of domain"
"OS error code  34:  Numerical result out of range"
"OS error code  35:  Resource deadlock avoided"
"OS error code  36:  File name too long"
"OS error code  37:  No locks available"
"OS error code  38:  Function not implemented"
"OS error code  39:  Directory not empty"
"OS error code  40:  Too many levels of symbolic links"
"OS error code  42:  No message of desired type"
"OS error code  43:  Identifier removed"
"OS error code  44:  Channel number out of range"
"OS error code  45:  Level 2 not synchronized"
"OS error code  46:  Level 3 halted"
"OS error code  47:  Level 3 reset"
"OS error code  48:  Link number out of range"
"OS error code  49:  Protocol driver not attached"
"OS error code  50:  No CSI structure available"
"OS error code  51:  Level 2 halted"
"OS error code  52:  Invalid exchange"
"OS error code  53:  Invalid request descriptor"
"OS error code  54:  Exchange full"
"OS error code  55:  No anode"
"OS error code  56:  Invalid request code"
"OS error code  57:  Invalid slot"
"OS error code  59:  Bad font file format"
"OS error code  60:  Device not a stream"
"OS error code  61:  No data available"
"OS error code  62:  Timer expired"
"OS error code  63:  Out of streams resources"
"OS error code  64:  Machine is not on the network"
"OS error code  65:  Package not installed"
"OS error code  66:  Object is remote"
"OS error code  67:  Link has been severed"
"OS error code  68:  Advertise error"
"OS error code  69:  Srmount error"
"OS error code  70:  Communication error on send"
"OS error code  71:  Protocol error"
"OS error code  72:  Multihop attempted"
"OS error code  73:  RFS specific error"
"OS error code  74:  Bad message"
"OS error code  75:  Value too large for defined data type"
"OS error code  76:  Name not unique on network"
"OS error code  77:  File descriptor in bad state"
"OS error code  78:  Remote address changed"
"OS error code  79:  Can not access a needed shared library"
"OS error code  80:  Accessing a corrupted shared library"
"OS error code  81:  .lib section in a.out corrupted"
"OS error code  82:  Attempting to link in too many shared libraries"
"OS error code  83:  Cannot exec a shared library directly"
"OS error code  84:  Invalid or incomplete multibyte or wide character"
"OS error code  85:  Interrupted system call should be restarted"
"OS error code  86:  Streams pipe error"
"OS error code  87:  Too many users"
"OS error code  88:  Socket operation on non-socket"
"OS error code  89:  Destination address required"
"OS error code  90:  Message too long"
"OS error code  91:  Protocol wrong type for socket"
"OS error code  92:  Protocol not available"
"OS error code  93:  Protocol not supported"
"OS error code  94:  Socket type not supported"
"OS error code  95:  Operation not supported"
"OS error code  96:  Protocol family not supported"
"OS error code  97:  Address family not supported by protocol"
"OS error code  98:  Address already in use"
"OS error code  99:  Cannot assign requested address"
"OS error code 100:  Network is down"
"OS error code 101:  Network is unreachable"
"OS error code 102:  Network dropped connection on reset"
"OS error code 103:  Software caused connection abort"
"OS error code 104:  Connection reset by peer"
"OS error code 105:  No buffer space available"
"OS error code 106:  Transport endpoint is already connected"
"OS error code 107:  Transport endpoint is not connected"
"OS error code 108:  Cannot send after transport endpoint shutdown"
"OS error code 109:  Too many references: cannot splice"
"OS error code 110:  Connection timed out"
"OS error code 111:  Connection refused"
"OS error code 112:  Host is down"
"OS error code 113:  No route to host"
"OS error code 114:  Operation already in progress"
"OS error code 115:  Operation now in progress"
"OS error code 116:  Stale NFS file handle"
"OS error code 117:  Structure needs cleaning"
"OS error code 118:  Not a XENIX named type file"
"OS error code 119:  No XENIX semaphores available"
"OS error code 120:  Is a named type file"
"OS error code 121:  Remote I/O error"
"OS error code 122:  Disk quota exceeded"
"OS error code 123:  No medium found"
"OS error code 124:  Wrong medium type"
"OS error code 125:  Operation canceled"
"OS error code 126:  Required key not available"
"OS error code 127:  Key has expired"
"OS error code 128:  Key has been revoked"
"OS error code 129:  Key was rejected by service"
"OS error code 130:  Owner died"
"OS error code 131:  State not recoverable"
"MySQL error code 132: Old database file"
"MySQL error code 133: No record read before update"
"MySQL error code 134: Record was already deleted (or record file crashed)"
"MySQL error code 135: No more room in record file"
"MySQL error code 136: No more room in index file"
"MySQL error code 137: No more records (read after end of file)"
"MySQL error code 138: Unsupported extension used for table"
"MySQL error code 139: Too big row"
"MySQL error code 140: Wrong create options"
"MySQL error code 141: Duplicate unique key or constraint on write or update"
"MySQL error code 142: Unknown character set used"
"MySQL error code 143: Conflicting table definitions in sub-tables of MERGE table"
"MySQL error code 144: Table is crashed and last repair failed"
"MySQL error code 145: Table was marked as crashed and should be repaired"
"MySQL error code 146: Lock timed out; Retry transaction" 
"MySQL error code 147: Lock table is full; Restart program with a larger locktable"
"MySQL error code 148: Updates are not allowed under a read only transactions" 
"MySQL error code 149: Lock deadlock; Retry transaction" 
"MySQL error code 150: Foreign key constraint is incorrectly formed" 
"MySQL error code 151: Cannot add a child row" 
"MySQL error code 152: Cannot delete a parent row"
--------------------- 

参考原文:https://blog.csdn.net/IAMoldpan/article/details/79163325

shell命令单行和多行注释

单行注释

# echo "hello world"

多行注释

#!/bin/bash
# author: lne
# url: lne.cc

echo "hello world"

# v1
: '
echo "hello world"
echo 'hello world'
echo `ls -l; mkdir 1`
'
# v2
:<<"eof"
echo "hello world"
echo 'hello world'
echo `ls -l; mkdir 1`
eof

# v3
:<<'!'
echo "hello world!"
echo 'hello world'
echo `ls -l; mkdir 1`
!false
!

# v4
:<<\EOF
echo "hello world!"
echo 'hello world'
echo `ls -l; mkdir 1`
EOF

# v5 注释中不能有单独"}", 包含可导致语法错误
((0)) && {
echo "hello world!"
echo 'hello world'
echo `ls -l; mkdir 1`
}

# v5 注释中不能有单独"}", 包含可导致语法错误
false && {
echo "hello world!"
echo 'hello world'
echo `ls -l; mkdir 1`
}

# v6 注释中不能有单独"fi", 包含可导致语法错误
if false; then
echo "hello world!"
echo 'hello world'
echo `ls -l; mkdir 1`
fi

# v7 注释中不能有单独"done", 包含可意外结束循环
while false; do
echo "hello world!"
echo 'hello world'
echo `ls -l; mkdir 1`
done

# v8 注释中不能有单独"done", 包含可意外结束循环
while :; do
break
echo "hello world!"
echo 'hello world'
echo `ls -l; mkdir 1`
done

# v9 注释中不能有单独"done", 包含可意外结束循环
for false; do
echo "hello world!"
echo 'hello world'
echo `ls -l; mkdir 1`
done

# v10 注释中不能有单独"done", 包含可意外结束循环
for ((;;)); do
break
echo "hello world!"
echo 'hello world'
echo `ls -l; mkdir 1`
done

# v11 注释中不能有单独"done", 包含可意外结束循环
until true; do
echo "hello world!"
echo 'hello world'
echo `ls -l; mkdir 1`
done

# v12 注释中不能有单独"done", 包含可意外结束循环
until :; do
echo "hello world!"
echo 'hello world'
echo `ls -l; mkdir 1`
done

# v13 注释中不能有单独"EOF", 包含可意外结束循环
<<"EOF"
echo "hello world!"
echo 'hello world'
echo `ls -l; mkdir 1`
EOF

# v14 将注释放到函数中
function comment() {
echo "hello world!"
echo 'hello world'
echo `ls -l; mkdir 1`
}


Linux终端彩色文字输出

1. 格式

echo "\033[字背景颜色;字体颜色m字符串\033[0m"

例如:

echo "\033[41;36m something here \033[0m"

其中41的位置代表底色, 36的位置是代表字的颜色

那些ascii code 是对颜色调用的始末.
\033[ ; m …… \033[0m

2.颜色

字背景颜色范围:40-49 字颜色范围:30-39
40:黑
41:深红
42:绿
43:黄色
44:蓝色
45:紫色
46:深绿
47:白色
30:黑
31:红
32:绿
33:黄
34:蓝色
35:紫色
36:深绿
37:白色

3.ANSI控制码的说明

\33[0m 关闭所有属性
\33[1m 设置高亮度
\33[4m 下划线
\33[5m 闪烁
\33[7m 反显
\33[8m 消隐
\33[30m – \33[37m 设置前景色
\33[40m – \33[47m 设置背景色
\33[nA 光标上移n行
\33[nB 光标下移n行
\33[nC 光标右移n行
\33[nD 光标左移n行
\33[y;xH设置光标位置
\33[2J 清屏
\33[K 清除从光标到行尾的内容
\33[s 保存光标位置
\33[u 恢复光标位置
\33[?25l 隐藏光标
\33[?25h 显示光标

代码演示:

#include <stdio.h>

int main(int argc,char **argv)
{
    printf(“\033[31m The ……\n\033[0m”);
    printf(“\033[2;7;1mHEOO.\n\033[2;7;0m”);
    printf(“\033[41;36msomthe here\n\033[0m”);
    return 0;
}

最大公约数和最小公倍数

欧几里德算法

欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数。其计算原理依赖于下面的定理:

定理:gcd(a,b) = gcd(b,a mod b)

证明:a可以表示成a = kb + r,则r = a mod b
假设d是a,b的一个公约数,则有
d|a, d|b,而r = a – kb,因此d|r
因此d是(b,a mod b)的公约数

假设d 是(b,a mod b)的公约数,则
d | b , d |r ,但是a = kb +r
因此d也是(a,b)的公约数

因此(a,b)和(b,a mod b)的公约数是一样的,其最大公约数也必然相等,得证

最小公倍数等于两个数的乘积除以最大公约数

/*
7-26 最大公约数和最小公倍数(15 分)

本题要求两个给定正整数的最大公约数和最小公倍数。
输入格式:

输入在一行中给出两个正整数M和N(≤1000)。
输出格式:

在一行中顺序输出M和N的最大公约数和最小公倍数,两数字间以1空格分隔。
输入样例:

511 292

输出样例:

73 2044

*/
#include <stdio.h>

int gcd(int, int);
int rec_gcd(int, int);
int lcm(int, int);
int main(void)
{
        int m, n;
        int g, l;
        scanf("%d%d", &m, &n);
        g = gcd(m, n);
        l = lcm(m, n);

        printf("%d %d\n", g, l);

        return 0;
}

int gcd(int u, int v)
{
        int r;
        while (v) {
                r = u % v;
                u = v;
                v = r;
        }

        return u;
}

int rec_gcd(int u, int v)
{
        if (!v)
                return u;
        else
                return rec_gcd(v, u % v);
}

int lcm(int u, int v)
{
        int r = gcd(u, v);
//      int l = u * (v / r);
//      int l = v * (u / r);
        int l = u * v / r;

        return l;
}


运行结果:

[lhf@lhf programming]$ ./26
511 292
73 2044

CentOS6系统”libc.so.6: version ‘GLIBC_2.15’ not found”解决方法

出现”libc.so.6: version ‘GLIBC_2.15’ not found”问题,是由于glibc版本过低,升级glibc即可。
可以将解决CentOS系统GLIBC_2.12版本低的问题
由于CentOS系统RPM源目前glibc最高版本是2.12,所以只能采用源码升级
一、查看系统glibc支持的版本

    strings /lib64/libc.so.6 |grep GLIBC  
    #查看当前glibc版本  
    ll /lib64/libc.so.6   
    lrwxrwxrwx. 1 root root 12 Oct  9  2014 /lib64/libc.so.6 -> libc-2.12.so  

二、下载glibc源码包

    cd /usr/src/  
    wget http://mirror.bjtu.edu.cn/gnu/glibc/glibc-2.15.tar.gz  
    wget http://mirror.bjtu.edu.cn/gnu/glibc/glibc-ports-2.15.tar.gz      #glibc插件  

三、解压安装升级glibc

    tar zxvf glibc-2.15.tar.gz  
    tar zxvf glibc-ports-2.15.tar.gz  
    mv glibc-ports-2.15 glibc-2.15/ports  
    mkdir build  
    cd build  
    ../glibc-2.15/configure  --prefix=/usr --disable-profile --enable-add-ons --with-headers=/usr/include --with-binutils=/usr/bin  
    make -j4  
    make install  

四、验证

查看当前系统glibc支持的版本

    strings /lib64/libc.so.6 |grep GLIBC  

五、误删libc.so.6解决办法

命令行执行如下:

LD_PRELOAD=/lib/libc-2.12.so ln -s /lib/libc-2.12.so lib/libc.so.6

来源:http://blog.csdn.net/hnhuangyiyang/article/details/50392997

实现C语言的继承和多态

较经典的动物世界中的实例来举例:
假设动物们(包括人)都会吃(Eat),会走(Walk),会说(Talk),而派生类为 dog(汪星人) 和 cat(喵星人),当然还可以是更多,dog 和 cat 都有自己独特的 eat, walk 和 talk 方式,那么大致的代码如下:
代码无警告编译运行

基类头文件

// animal.h
#ifndef ANIMAL_H__
#define ANIMAL_H__

typedef struct animal_s_ animal_t;
typedef struct animal_ops_s_ animal_ops_t;

struct animal_s_ {
        char *name;
        animal_ops_t *animal_ops;
};

struct animal_ops_s_ {
        void (*eat)(char *);
        void (*walk)(int);
        void (*talk)(char *);
};

animal_t *animal_init(char *name);

void animal_eat(animal_t *, char *food);
void animal_walk(animal_t *, int steps);
void animal_talk(animal_t *, char *msg);

void animal_die(animal_t *);

#endif//ANIMAL_H__

基类实现

// animal.c
#include <assert.h>
#include <stdlib.h>
#include <string.h>

#include "animal.h"

animal_t *animal_init(char *name)
{
        assert(name != NULL);
        size_t name_len = strlen(name);

        animal_t *animal = malloc(sizeof (*animal)
                + sizeof (animal_ops_t) + name_len + 1);
        memset(animal, 0, sizeof (*animal)
                + sizeof (animal_ops_t) + name_len + 1);
        animal->name = (char *)animal
                + sizeof (animal_t) + sizeof (animal_ops_t);
        memcpy(animal->name, name, name_len);
        animal->animal_ops = (animal_ops_t *)((char *)animal
                + sizeof (*animal));

        return animal;
}

void animal_eat(animal_t *animal, char *food)
{
        animal->animal_ops->eat(food);

        return;
}

void animal_walk(animal_t *animal, int steps)
{
        animal->animal_ops->walk(steps);

        return;
}

void animal_talk(animal_t *animal, char *msg)
{
        animal->animal_ops->talk(msg);

        return;
}

void animal_die(animal_t *animal)
{
        assert(animal != NULL);
        free(animal);

        return;
}

继续阅读

CentOS 6通过yum安装GCC4.7 4.8 4.9 5.3 6.3 7.3等高版本GCC

再说一下已经停止支持的devtoolset4(gcc 5.2)及之前版本的安装方法
gcc 4.7 安装

    wget http://people.centos.org/tru/devtools-1/devtools-1.repo -O /etc/yum.repos.d/devtoolset-1.repo
    yum install centos-release-scl
    yum install devtoolset-1-toolchain
    scl enable devtoolset-1 bash
    gcc --version

gcc 4.8 安装

curl -Lks http://www.hop5.in/yum/el6/hop5.repo > /etc/yum.repos.d/hop5.repo
yum install gcc gcc-g++
gcc --version

或者使用官方工具

    wget http://people.centos.org/tru/devtools-2/devtools-2.repo -O /etc/yum.repos.d/devtoolset-2.repo
    yum install centos-release-scl
    yum install devtoolset-2-toolchain
    scl enable devtoolset-2 bash
    gcc --version

gcc 4.9 安装

    yum install centos-release-scl
    yum install devtoolset-3-toolchain
    scl enable devtoolset-3 bash
    gcc --version

gcc 5.3 安装

    yum install centos-release-scl
    yum install devtoolset-4-toolchain
    scl enable devtoolset-4 bash
    gcc --version

gcc 6.3 安装

    yum install centos-release-scl
    yum install devtoolset-6-toolchain
    scl enable devtoolset-6 bash
    gcc --version

gcc 7.3 安装

    yum install centos-release-scl
    yum install devtoolset-7-toolchain
    scl enable devtoolset-7 bash
    gcc --version

scl enable devtoolset-3 bash 只是临时覆盖系统原有的GCC引用,重启后恢复原来的GCC,如果想永久覆盖,
可在/etc/bashrc文件中添加

source /opt/rh/devtoolset-3/enable

或者:如果root单独使用,在/root/.bashrc文件中添加

source /opt/rh/devtoolset-3/enable

重启后生效。

文件参考: https://www.quyu.net/info/876.html
https://www.dwhd.org/20160724_085212.html

C语言输出菱形图像

使用C语言输出如下图像

思路:确定每行开始位置,和该行’*’的总个数。
代码文件,该图形可以整体右移,请调节 #define LEFT 1 参数

[lhf@localhost work]$ cat diamond.c
/**
 *     *
 *    * *
 *   * * *
 *    * *
 *     *
 */
#include <stdio.h>
#define LEFT 1  // right shift space

int main(void)
{
        int size = 0;
        printf("Input side number of diamond: ");
        scanf("%d", &size);
        int len = size * 2 - 1;
        int high = len;
        int width = len + LEFT;
        char diamond[high][width];

        const char dia = '*';
        const char pidd = ' ';

//      begin of start and number of diamond
        int begin = 0, n;
        for (int i = 0; i < high; i++) {
                if (i < size) {
                        begin = size - i - 1 + LEFT;
                        n = i + 1;
                } else {
                        begin = i - size + 1;
                        n = size - begin;
                        begin += LEFT;
                }
                for (int j = 0; j < width; j++) {
                        if ((j == begin) && (n--)) {
                                putchar(dia);
                                begin += 2;
                        } else
                                putchar(pidd);
                }
                putchar('\n');
        }

        return 0;
}

Linux下的tar压缩解压缩命令详解

tar

-c: 建立压缩档案
-x:解压
-t:查看内容
-r:向压缩归档文件末尾追加文件
-u:更新原压缩包中的文件

这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个。下面的参数是根据需要在压缩或解压档案时可选的。

-z:有gzip属性的
-j:有bz2属性的
-Z:有compress属性的
-v:显示所有过程
-O:将文件解开到标准输出

下面的参数-f是必须的

-f: 使用档案名字,切记,这个参数是最后一个参数,后面只能接档案名。

# tar -cf all.tar *.jpg
这条命令是将所有.jpg的文件打成一个名为all.tar的包。-c是表示产生新的包,-f指定包的文件名。

# tar -rf all.tar *.gif
这条命令是将所有.gif的文件增加到all.tar的包里面去。-r是表示增加文件的意思。

# tar -uf all.tar logo.gif
这条命令是更新原来tar包all.tar中logo.gif文件,-u是表示更新文件的意思。

# tar -tf all.tar
这条命令是列出all.tar包中所有文件,-t是列出文件的意思

# tar -xf all.tar
这条命令是解出all.tar包中所有文件,-t是解开的意思

压缩

tar -cvf jpg.tar *.jpg //将目录里所有jpg文件打包成jpg.tar

tar -czf jpg.tar.gz *.jpg   //将目录里所有jpg文件打包成jpg.tar后,并且将其用gzip压缩,生成一个gzip压缩过的包,命名为jpg.tar.gz

tar -cjf jpg.tar.bz2 *.jpg //将目录里所有jpg文件打包成jpg.tar后,并且将其用bzip2压缩,生成一个bzip2压缩过的包,命名为jpg.tar.bz2

tar -cZf jpg.tar.Z *.jpg   //将目录里所有jpg文件打包成jpg.tar后,并且将其用compress压缩,生成一个umcompress压缩过的包,命名为jpg.tar.Z

rar a jpg.rar *.jpg //rar格式的压缩,需要先下载rar for linux

zip jpg.zip *.jpg //zip格式的压缩,需要先下载zip for linux

解压

tar -xvf file.tar //解压 tar包

tar -xzvf file.tar.gz //解压tar.gz

tar -xjvf file.tar.bz2   //解压 tar.bz2

tar -xZvf file.tar.Z   //解压tar.Z

unrar e file.rar //解压rar

unzip file.zip //解压zip

总结

1、*.tar 用 tar -xvf 解压

2、*.gz 用 gzip -d或者gunzip 解压

3、*.tar.gz和*.tgz 用 tar -xzf 解压

4、*.bz2 用 bzip2 -d或者用bunzip2 解压

5、*.tar.bz2用tar -xjf 解压

6、*.Z 用 uncompress 解压

7、*.tar.Z 用tar -xZf 解压

8、*.rar 用 unrar e解压

9、*.zip 用 unzip 解压

c++ primer 5 exercise 10.9

c++ primer 5 exercise 10.9

实现自己的elimDups程序。测试你的程序,分别在读取输入后、调用unique后以及调用erase后打印vector
程序实现:

// sort and elimDups
#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;
using std::unique;

vector<string> strToVec(const string line, const string separators);
void show(vector<string> const &words);
void elimDups(vector<string> words);
int main(void)
{
        const string Separators = " \t:;,\n\r\f\v";
        string str("the quick red fox jumps the slow red turtle");

        // string turn on vector<sting>
        vector<string> vec;
        vec = strToVec(str, Separators);

        // show vector<string>
        cout << "string to vector<sting>..." << endl;
        show(vec);

        // elimDups()
        cout << "\nelimDups()..." << endl;
        elimDups(vec);

        return 0;
}

vector<string> strToVec(const string line, const string separators)
{
        vector<string> vec;
        string word;
        std::string::size_type startPos = 0, endPos = 0;
        std::string::size_type wordLen = 0;

        while ((startPos = line.find_first_not_of(separators,
                endPos)) != std::string::npos) {
                endPos = line.find_first_of(separators, startPos);
                if (endPos == std::string::npos)
                        wordLen = line.size() - startPos;
                else
                        wordLen = endPos - startPos;
                word.assign(line.begin() + startPos,
                        line.begin() + startPos + wordLen);
                vec.push_back(word);
        }

        return vec;
}

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

        return;
}

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);

        return;
}

程序测试结果:

[lhf@localhost 10-9]$ ./elimDups
string to vector<sting>...
the quick red fox jumps the slow red turtle

elimDups()...
fox jumps quick red red slow the the turtle
fox jumps quick red slow the turtle the red
fox jumps quick red slow the turtle