Chinaunix首页 | 论坛 | 博客
  • 博客访问: 824874
  • 博文数量: 109
  • 博客积分: 650
  • 博客等级: 上士
  • 技术积分: 1483
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-01 17:29
文章分类

全部博文(109)

文章存档

2016年(5)

2015年(21)

2014年(16)

2013年(38)

2012年(29)

分类: LINUX

2013-03-16 10:13:31

[omcbo@DGSERNMC111 C]$ more fig.cpp 
#include
#include
#include
#include
using namespace::std;
int main()
{
        const int arraysize = 7;
        int frequency[ arraysize ] = {0};
        srand( time (0));///让每次得到的数据不一样
        for (int roll = 1; roll <= 6000000; roll++)
        frequency[ 1+ rand()%6 ]++;
        cout<<"Face"<
        for ( int face =1; face < arraysize; face++)
        cout<
        return 0;
}
[omcbo@DGSERNMC111 C]$ g++ fig.cpp -o fig
[omcbo@DGSERNMC111 C]$ ./fig
Face    Frequency
   1       999116
   2       999647
   3      1000663
   4       999481
   5      1001452
   6       999641

韶关
#include  
#include  
#include   //time_t
#include    
#include   //fork
#include    //fork
#include    //open 
#include     //open
#include     //open
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  
#include  

using namespace std;

string  BSC_PATH,BSC_NAME;
int  BSC_NAME_LENGTH;

int main(int argc,char **argv)  
{  
struct sigaction act;  
int error,in,out;  
time_t now;  
int memory;
long int M15=0;
FILE* f;
char buffer[1024]={0};
while(1)  
{  
    time_t  timep,timers;
    struct  tm tblock;
    struct  tm tnow;
    struct  tm files_wait;

    time(&timep);
    printf("%s",asctime(gmtime(&timep)));
    tnow= *localtime(&timep);

    int NowHour=0,NowMin=0,Mon=0,Day=0,Yers=0;
    Yers=tnow.tm_year+1900;
    Mon=tnow.tm_mon+1;
    Day=tnow.tm_mday;
    NowHour=tnow.tm_hour;
    NowMin=tnow.tm_min;
    int FIAG = 0;
    cout<<"NOW time is Year:"<
    if (  NowMin == 59  )//|| (NowHour == 15 && NowMin == 29) || (NowHour == 23 && NowMin == 59))
    {
   f=popen("((ss=`date +%s/900*900`));echo $ss","r");
   if (f==NULL)
   {
   return 1;
   }
   if (fscanf(f,"%ld",$M15)!=1)
   {
   return 2;
   }
   cout<
   sprintf(buffer,"/home/omcbo/JmPfmMon/bin/SystemServer/RunRLDCPII.sh %ld >/dev/null 2>&1",M15)
       system(buffer);
       cout<<"end"<
       string CMD = "date >> /home/omcbo/RunRLDCPII.sh.log";
       system(CMD.c_str());
       sleep(60);
    }
    else
{
sleep(50);
}
    sleep(10);  
}  

exit(0);  
}
二元作用域分辨运算符(::)
重载符号除. .* :: ?.之外都可以重载
[omcbo@DGSERNMC111 test1]$ vim op.cpp
#include
#include
#include
using namespace std;
int main(void)
{
        string area;
        cin.ignore(2);//忽略前两个字符 后再读取
        cin>>setw(5)>>area;//读取5个字符
        cout<
        return 0;
}
[omcbo@DGSERNMC111 test1]$ ./op
abcdefghijklmm^Hn
cdefg
#############################################################################
[omcbo@DGSERNMC111 C]$ more timeout.cpp
#include
#include
#include
#include
#include
using namespace std;
int main (int argc,char *argv[])
{
        struct timeval tpstart,tpend;
        float timeuse;
        string CMD=argv[1];
        cout<
        gettimeofday(&tpstart,NULL);
        system(CMD.c_str());
        gettimeofday(&tpend,NULL);
        timeuse=1000000*(tpend.tv_sec-tpstart.tv_sec)+tpend.tv_usec-tpstart.tv_usec;
// tv_sec以秒为单位
        timeuse/=1000000;
        printf("use time:%f\n",timeuse);
        return 0;
}
[omcbo@DGSERNMC111 C]$ ./timeout "./ggg"
./ggg
Hello World!
12
Hello World!
12
eeef ffrfr efef
eeef
 ffrfr efef
1aaaaa
2aaaaa
3aaaaa
4aaaaa
5aaaaa
use time:12.011116
[omcbo@DGSERNMC111 C]$ more ggg.c
#include
#include
#include
using namespace std;
int main(void)
{
        string s = "Hello World!";
        cout<< s.c_str()<
        cout<

        cout<
        cout<

        char a[100];
        cin>>a;
        cout<
        char c[100];
        cin.getline(c,sizeof(c),'\n');//可以取出空格
        cout<

        //string RE(2+3i);
        //cout<
        if(access("./aaa.sh",R_OK)==0);
        {
                string cmd="./aaa.sh";
                system(cmd.c_str());
        }

        file1.open("aaa.sh",ios::binary|ios::in,0);

        return 0;
}
############################################################################
Vector的使用,数组
[omcbo@DGSERNMC111 C]$ more vector.cpp 
#include
#include
using namespace::std;
using std::vector;
using std::cout;
int main(void)
{
        vector inters1(7);
        size_t i;
        for (i = 0;i < inters1.size();i++)
        cin>>inters1[i];
        for (i = 0;i < inters1.size();i++)
        cout<
        cout<
        inters1.at(2)=88;
        inters1[8]=99;
        for (i = 0;i < inters1.size();i++)
        cout<
        cout<
        return 0;
}

[omcbo@DGSERNMC111 C]$ ./vector1 
1 2 3 4 5 6 7 8 9 0 0 8 y  4 3
1234567
12884567
#############################################################################
1. 获取文件名,然后输出到数组
2. [omcbo@DGSERNMC111 C]$ cat GetFileName.cpp
3. #include
4. #include
5. #include
6. #include
7. #include
8. using namespace std;
9. #define OFile file
10. #define MAXLINE 100
11. int main()
12. {
13. FILE *fp;
14. char arr[MAXLINE+1];
15. int i=0;
16. int j=0;
17.
18. struct node{
19. char str[MAXLINE+1];
20. }data[10];
21.
22. if(access("file",F_OK) != 0)
23. {
24.         cout<<"File do not exist"<
25.         exit (1);
26. }
27. if((fp=fopen("file","r")) == NULL )
28. {
29.         cout<<"file can not be read"<
30.         exit (2);
31. }
32. while((fgets(arr,MAXLINE,fp))!=NULL)
33. {
34.         if(arr[strlen(arr)-1]=='\n')
35.         arr[strlen(arr)-1]=0;
36.         if(arr[strlen(arr)-1]=='\r')
37.         arr[strlen(arr)-1]=0;
38.         strcpy(data[i].str,arr);
39.         i++;
40. }
41. for (j=0;j<=i;j++)
42. {
43.
44.         cout<
45. }
46.
47. return 0;
48. }
49. g++ GetFileName.cpp -o GetFileName
50. [omcbo@DGSERNMC111 C]$ ./GetFileName 
51. PROC_ALEX_CELL_60_01
52. PROC_ALEX_CELL_60_02
53. PROC_ALEX_CELL_60_03
54. PROC_ALEX_CELL_60_04
55. PROC_ALEX_CELL_60_05
56. PROC_ALEX_CELL_60_06
57. PROC_ALEX_CELL_60_07
58. PROC_ALEX_CELL_60_08
59. PROC_ALEX_CELL_60_09
60. [omcbo@DGSERNMC111 C]$ cat file 
61. PROC_ALEX_CELL_60_01
62. PROC_ALEX_CELL_60_02
63. PROC_ALEX_CELL_60_03
64. PROC_ALEX_CELL_60_04
65. PROC_ALEX_CELL_60_05
66. PROC_ALEX_CELL_60_06
67. PROC_ALEX_CELL_60_07
68. PROC_ALEX_CELL_60_08
69. PROC_ALEX_CELL_60_09
###########################################################################
Double四舍五入
[omcbo@DGSERNMC111 C]$ more atof.c 
#include
#include
int main(void)
{

        double a=123.45;
        double b=-1234.56;
        char ptr[25];
        gcvt(a,4,ptr);
        printf("a value=%s\n",ptr);
        return 0;
}
[omcbo@DGSERNMC111 C]$ ./atof 
a value=123.5
##########################################################################
运行选项./getopt -a
while((result=getopt(argc,argv,"a"))!= -1)
    {
           switch(result)
          {
              case 'a':
                  printf("option=a, optopt=%c, optarg=%s\n", optopt, optarg);
                  break;
              case 'b':
                   printf("option=b, optopt=%c, optarg=%s\n", optopt, optarg);
                   break;
              case 'c':
                  printf("option=c, optopt=%c, optarg=%s\n", optopt, optarg);
                    break;
              case '?':
                    printf("result=?, optopt=%c, optarg=%s\n", optopt, optarg);
                    break;
            default:
                  printf("default, result=%c\n",result);
                  break;
          }
#############################################################################
gets从终端读入是的字符串是用\0结束的,而fgets是以\n结束的(一般输入都用ENTER结束),然后strcmp两者的时候是不会相等的。
建议可以在linux终端下,利用dos2unix filename,来将换行符处理一下!
Fgets的使用:
#include
#include
int main()
{
        char buff[60];
        printf("pls input a string:");
        fgets(buff,100,stdin);
        printf("\nyour string is : %s\n",buff);
        return 0;
}
#include "stdio.h"
#include
void main()
{
    int a[3][3]={{20,25,18},{15,26,9},{34,14,27}};
    int temp[3];
    int k,t;
    memcpy(temp,a[1],sizeof(int));
    memcpy(a[1],a[0],sizeof(int));
    memcpy(a[0],temp,sizeof(int));
    for(k=0;k<3;k++)
      {for(t=0;t<3;t++)
        printf(" %d ",a[k][t]);
      printf("\n");   
     }
}

//memset(LocalChr,0,sizeof(LocalChr));
//memcpy(LocalChr,LineName.c_str(),LineName.length());
###################################################################33
#include
标准C++定义了这个文件。
里面有一个size()函数,可以返回字符串的长度。用法:
string a;
a="HELLO";
cout<
其实还有一些替代的方法,例如用字符数组了。
C或者C++也提供了对他的读取操作。但是,和其他语言不太相同的是,读取字符数组时,见到空格就停止读取。
例如:(C++)
char c[100];
cin>>c;
cout<
(C)
char c[100];
scanf("%s",c);//由于c本身就是指针(数组名就是指向数组首元素的指针),所以不用&操作符取地址。
printf("%s\n",c);
可以这么直接的读入。但是,当输入
asdghaworgh       aworgh时,却只能输出asdghaworgh而不能输出asdghaworgh       aworgh。想要改变这个现状,
可以运用getline函数。
用法:
char c[100];
cin.getline(c,sizeof(c),'\n');

string s1(“happy”);
string s2(“birthday”);
string s3;
可用==,!=,<,>,=<,=>比较
If(S3.empty())判断是否为空
S3=s1//赋值
S1 +=s2;//连接
S1.substr(0,14);//截取0到14工15个字符
S1.substr(15);//截取第16个字符后面的字符
String *s4Ptr=new string(s1);
S1[0],  s1.at(0)两种方法

string strtest="Hello " + strinfo + " Welcome" + " to China" + " !";

看见其中的特点了吗?只要你的等式里面有一个 string 对象,你就可以一直连续"+",但有一点需要保证的是,在开始的两项中,必须有一项是 string 对象。其原理很简单:

系统遇到"+"号,发现有一项是string 对象。
系统把另一项转化为一个临时 string 对象。
执行 operator + 操作,返回新的临时string 对象。
如果又发现"+"号,继续第一步操作。

由于查找是使用最为频繁的功能之一,string 提供了非常丰富的查找函数。其列表如下:
函数名 描述 find 查找 rfind 反向查找 find_first_of 查找包含子串中的任何字符,返回第一个位置 find_first_not_of 查找不包含子串中的任何字符,返回第一个位置 find_last_of 查找包含子串中的任何字符,返回最后一个位置 find_last_not_of 查找不包含子串中的任何字符,返回最后一个位置以上函数都是被重载了4次,以下是以find_first_of 函数为例说明他们的参数,其他函数和其参数一样,也就是说总共有24个函数:
[omcbo@DGSERNMC111 C]$ more string2.cpp 
#include
#include
using namespace std;
int main(){
string strinfo=" //*---Hello Word!......------";
string strset="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int first = strinfo.find_first_of(strset);//匹配英文字母
if(first == string::npos) {
cout<<"not find any characters"<
return -1;
}
int last = strinfo.find_last_of(strset);
if(last == string::npos) {
cout<<"not find any characters"<
return -1;
}
cout << strinfo.substr(first, last - first + 1)<
return 0;
}
[omcbo@DGSERNMC111 C]$ ./string2 
Hello Word
1.  如果string sub = ”abc“;
              string s = ”cdeabcigld“;
     s.find(sub) , s.rfind(sub) 这两个函数,如果完全匹配,才返回匹配的索引,即:当s中含有abc三个连续的字母时,才返回当前索引。
     s.find_first_of(sub),   s.find_first_not_of(sub),   s.find_last_of(sub),  s.find_last_not_of(sub)  这四个函数,查找s中含有sub中任意字母的索引。
2. 如果没有查询到,则返回string::npos,这是一个很大的数,其值不需要知道。

#include
#include
using namespace std;
void string_replace(string &strBig,const string &strsrc,const string &strdst) 
{
string::size_type pos=0;
string::size_type srclen=strsrc.size();
string::size_type dstlen=strdst.size();
while( (pos=strBig.find(strsrc, pos)) != string::npos)
{
        strBig.replace(pos, srclen, strdst);
        pos += dstlen;
}
}
/*此处也可以用while( (pos=strBig.find(strsrc, pos)) != string::npos){
strBig.erase(pos, srclen);
strBig.insert(pos, strdst);
pos += dstlen;
}
*/
int main(void) 
{
string strinfo="This is Winter, Winter is a programmer. Do you know Winter?";
cout<<"Orign string is :/n"<
string_replace(strinfo, "Winter", "wende");
cout<<"After replace Winter with wende, the string is :/n"<
return 0;
}
[omcbo@DGSERNMC111 C]$ ./string3 
Orign string is :/nThis is Winter, Winter is a programmer. Do you know Winter?
After replace Winter with wende, the string is :/nThis is wende, wende is a programmer. Do you know wende?

#include
#include
using namespace std;
int main(void)
{
        string s1;
        cin>>s1;
        cout<
        if (s1 == "hello")
        {
                cout<<"your are very polite\n";
        }
        else
        {
                cout<<"ok,nothing"<
        }
        s1 += ",welcome to china";
        cout << s1<
        string s2="how are  you?"+s1;
        for(int i=0;i
        {
                cout<
        }
        //find
        string s("1a2b3c4d5e6f7g8h9i1a2b3c4d5e6f7g8ha9i");  
        string flag="c";  
        string::size_type position;
        string::size_type pp;
        pp=s.find("a");
        position = s.find("a",pp+1);
        if (position != s.npos) 
        {  
                cout << "position is : " << position << endl;  
         }  
        else  
         {  
                cout << "Not found the flag" + flag;  
         }   

        return 0;
}
[omcbo@DGSERNMC111 C]$ ./string
hello       
hello
your are very polite
hello,welcome to china
how are  you?hello,welcome to china1eeee
position is : 1
////////find_first_of,
#include
#include
using namespace std;
int main(){
string strinfo=" //*---Hello Word!......------";
string strset="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
int first = strinfo.find_first_of(strset);
if(first == string::npos) {
cout<<"not find any characters"<
return -1;
}
int last = strinfo.find_last_of(strset);
if(last == string::npos) {
cout<<"not find any characters"<
return -1;
}
cout << strinfo.substr(first, last - first + 1)<
return 0;
}
[omcbo@DGSERNMC111 C]$ ./string2 
Hello Word


// basic_string_ctor.cpp
// compile with: /EHsc
#include
#include

int main( )
{
   using namespace std;

   // The first member function initializing with a C-string
   const char *cstr1a = "Hello Out There.";
   basic_string str1a ( cstr1a , 5);
   cout << "The string initialized by C-string cstr1a is: "
        << str1a << "." << endl;

   // The second member function initializing with a string
   string  str2a ( "How Do You Do?" );
   basic_string str2b ( str2a , 7 , 7 );
   cout << "The string initialized by part of the string cstr2a is: "
        << str2b << "." << endl;

   // The third member function initializing a string
   // with a number of characters of a specific value
   basic_string str3a ( 5, '9' );
   cout << "The string initialized by five number 9s is: "
        << str3a << endl;

   // The fourth member function creates an empty string
   // and string with a specified allocator
   basic_string str4a;
   string str4b;
   basic_string str4c ( str4b.get_allocator( ) );
   if (str4c.empty ( ) )
      cout << "The string str4c is empty." << endl;
   else
      cout << "The string str4c is not empty." << endl;

   // The fifth member function initializes a string from
   // another range of characters
   string str5a ( "Hello World" );
   basic_string str5b ( str5a.begin ( ) + 5 , str5a.end ( ) );
   cout << "The string initialized by another range is: "
        << str5b << "." << endl;
}
Output
The string initialized by C-string cstr1a is: Hello.
The string initialized by part of the string cstr2a is: You Do?.
The string initialized by five number 9s is: 99999
The string str4c is empty.
The string initialized by another range is:  World.


#include
 3 #include
 4 using namespace std;
 5 
 6 int main ()
 7 {
 8   string str1 ("green apple");
 9   string str2 ("red apple");
10 
11   if (str1.compare(str2) != 0)
12     cout << str1 << " is not " << str2 << "\n";
13 
14   if (str1.compare(6,5,"apple") == 0)
15     cout << "still, " << str1 << " is an apple\n";
16 
17   if (str2.compare(str2.size()-5,5,"apple") == 0)
18     cout << "and " << str2 << " is also an apple\n";
19 
20   if (str1.compare(6,5,str2,4,5) == 0)
21     cout << "therefore, both are apples\n";
22 
23   return 0;
24 }

###############################################################################
C++中的vector使用范例
一、概述
vector是C++标准模板库中的部分内容,它是一个多功能的,能够操作多种数据结构和算法的模板类和函数库。vector是一个容器,它能够存放各种类型的对象,简单地说,vector是一个能够存放任意类型的动态数组,可以动态改变大小。
例如:
// c语言风格
int myHouse[100] ;
// 采用vector
vector vecMyHouse(100);
当如上定义后,vecMyHouse就可以存放100个int型的数据了。
1. 它可以像普通数组一样访问
eg:  vecMyHouse[50] = 1024;
2. 你可以顺序地向容器中填充数据
eg:int i =0 ;
for( ;i< 25; i++ )
{
vecMyHouse.push_back(1);
}
    
3. 它还可以动态地改变它的大小,通过下面这条语句实现
// 将容器的大小改为400,这样容器中就可以容纳400个int型数据了
eg:vecMyHouse.resize(400);
4. 你也可以在容器中装入自定义的数据类型
eg:
// 自定义一个class
class Cmyclass
{
};
// 定义一个存放class的容器
vector vecMyHouse;
5. 你可以在定义容器时为它赋初值
// 定义一个容纳100个int型数据的容器,初值赋为0
vector vecMyHouse(100,0);
6. 你可以把一个容器的对象赋值给另外一个容器
eg:
// 定义一个容纳100个int型数据的容器,初值赋为0
vector vecMyHouse(100,0);
//  定义一个新的容器,内容与上述容器一样
vector myVec ;
myVec = vecMyHouse;
二、 以上是vector容器的简单介绍,下面将详细介绍它的其他功能:
1. 为了使用vector,必须在你的头文件中包含下面的代码:
#include
2. vector属于std命名域的,因此需要通过命名限定,可以在文件开头加上
using std::vector;
或者
using namespace std;
或者直接在使用vector的代码前加前缀
eg:
std::vector myHouse;
3. vector提供如下函数或操作:
下面列举了部分常用的功能
// 定义一个vector
std::vector c;
// 可以使用的功能
c.clear()         移除容器中所有数据。
c.empty()         判断容器是否为空。
c.erase(pos)        删除pos位置的数据
c.erase(beg,end) 删除[beg,end)区间的数据
c.front()         传回第一个数据。
c.insert(pos,elem)  在pos位置插入一个elem拷贝
c.pop_back()     删除最后一个数据。
c.push_back(elem) 在尾部加入一个数据。
c.resize(num)     重新设置该容器的大小
c.size()         回容器中实际数据的个数。
c.begin()           返回指向容器第一个元素的迭代器
c.end()             返回指向容器最后一个元素的迭代器
三、下面描述一下什么是迭代器
迭代器相当于指针,例如:
// 对于变量而言,使用指针指向对应的变量
// 以后就可以使用 * 加指针来操作该变量了
int a = 10;
int *p;
p = &a;
// 使用指针操作该变量
eg: *p = 11; // 操作后a变为 11
// 对于容器,使用迭代器操作容器中对应位置的值
// 当迭代器指向了容器中的某位置,则可以使用 * 加迭代器操作该位置了
// 定义一个vector
std::vector myVec;
//添加10个元素
for(int j =0 ; j<10 ; j++)
{

myVec.push_back(j);
}
// 定义一个迭代器
std::vector::iterator p;
// 指向容器的首个元素
p = myVec.begin();
// 移动到下一个元素
p ++;
// 修改该元素赋值
*p = 20 ;  //< 则myVec容器中的第二个值被修改为了20
// 循环扫描迭代器,改变所有的值
p = myVec.begin();
for( ; p!= myVec.end(); p++ )
{
*p = 50;
}
以上简单讲述了vector的用法,仅供入门之用,谢谢。
-------------------------------------------------------------------------------------
1.vector 的数据的存入和输出:
#include
#include
#include
using namespace std;
void main()
{
  int i = 0;
   vector v;
   for( i = 0; i < 10; i++ )
  {
            v.push_back( i );//把元素一个一个存入到vector中
  }
   对存入的数据清空
  for( i = 0; i < v.size(); i++ )//v.size() 表示vector存入元素的个数
  {
         cout << v[ i ] << " "; //把每个元素显示出来
  }
  cont << endl;
}
注:你也可以用v.begin()和v.end() 来得到vector开始的和结束的元素地址的指针位置。你也可以这样做:
vector::iterator iter;
for( iter = v.begin(); iter != v.end(); iter++ )
{
   cout << *iter << endl;
}
2. 对于二维vector的定义。
1)定义一个10个vector元素,并对每个vector符值1-10。
#include
#include
#include
using namespace std;
void main()
{
int i = 0, j = 0;
//定义一个二维的动态数组,有10行,每一行是一个用一个vector存储这一行的数据。
所以每一行的长度是可以变化的。之所以用到vector(0)是对vector初始化,否则不能对vector存入元素。
vector< vector > Array( 10, vector(0) );
for( j = 0; j < 10; j++ )
{
  for ( i = 0; i < 9; i++ )
  {
   Array[ j ].push_back( i );
  }
}
for( j = 0; j < 10; j++ )
{
  for( i = 0; i < Array[ j ].size(); i++ )
  {
   cout << Array[ j ][ i ] << " ";
  }
  cout<< endl;
}
}
2)定义一个行列都是变化的数组。
#include
#include
#include
using namespace std;
void main()
{
int i = 0, j = 0;
vector< vector > Array;
vector< int > line;
for( j = 0; j < 10; j++ )
{
  Array.push_back( line );//要对每一个vector初始化,否则不能存入元素。
  for ( i = 0; i < 9; i++ )
  {
   Array[ j ].push_back( i );
  }
}
for( j = 0; j < 10; j++ )
{
  for( i = 0; i < Array[ j ].size(); i++ )
  {
   cout << Array[ j ][ i ] << " ";
  }
  cout<< endl;
}
}
 
使用 vettor erase 指定元素
[omcbo@DGSERNMC111 C]$ vim vector1.cpp
#include
#include
using namespace std;
int  main()
{
   vector  arr;
   arr.push_back(6);
   arr.push_back(8);
   arr.push_back(3);
   arr.push_back(8);
   cout<<"before remove 8:\n";
   int i=0;
   for(i=0;i
    {
        cout<
     }
        cout<
   for(vector::iterator it=arr.begin(); it!=arr.end(); )
   {
       if(*it == 8)
       {
           it = arr.erase(it);
       }
       else
       {
           ++it;
       }
   }
   cout << "After remove 8:\n";
   for(vector::iterator it = arr.begin(); it < arr.end(); ++it)
   {
       cout << *it << " ";
   }
   cout << endl;
}
[omcbo@DGSERNMC111 C]$ ./vector2
before remove 8:
6 8 3 8 
After remove 8:
6 3
###############################################################################

access(判断是否具有存取文件的权限)  
相关函数  stat,open,chmod,chown,setuid,setgid
 
表头文件  #include
 
定义函数  int access(const char * pathname,int mode);
 
函数说明  access()会检查是否可以读/写某一已存在的文件。参数mode有几种情况组合,
R_OK,W_OK,X_OK 和F_OK。R_OK,W_OK与X_OK用来检查文件是否具有读取、写入和执行的权限。
F_OK则是用来判断该文件是否存在。由于access()只作权限的核查,并不理会文件形态或文件内容,
因此,如果一目录表示为“可写入”,表示可以在该目录中建立新文件等操作,而非意味此目录可以被当做文件处理。
例如,你会发现DOS的文件都具有“可执行”权限,但用execve()执行时则会失败。
 
返回值  若所有欲查核的权限都通过了检查则返回0值,表示成功,只要有一权限被禁止则返回-1。
 
错误代码  EACCESS 参数pathname 所指定的文件不符合所要求测试的权限。
EROFS 欲测试写入权限的文件存在于只读文件系统内。
EFAULT 参数pathname指针超出可存取内存空间。
EINVAL 参数mode 不正确。
ENAMETOOLONG 参数pathname太长。
ENOTDIR 参数pathname为一目录。
ENOMEM 核心内存不足
ELOOP 参数pathname有过多符号连接问题。
EIO I/O 存取错误。
 
附加说明  使用access()作用户认证方面的判断要特别小心,
例如在access()后再做open()的空文件可能会造成系统安全上的问题。
 
范例  /* 判断是否允许读取/etc/passwd */
#include
int main()
{
if (access(“/etc/passwd”,R_OK) = =0)
printf(“/etc/passwd can be read\n”);
}
 
执行  /etc/passwd can be read

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
assert宏的原型定义在中,其作用是如果它的条件返回错误,则终止程序执行, 
#include
#include
#include
int main( void )
{
       FILE *fp;
    
       fp = fopen( "test.txt", "w" );//以可写的方式打开一个文件,如果不存在就创建一个同名文件
       assert( fp );                           //所以这里不会出错
       fclose( fp );
    
       fp = fopen( "noexitfile.txt", "r" );//以只读的方式打开一个文件,如果不存在就打开文件失败
       assert( fp );                           //所以这里出错
       fclose( fp );                           //程序永远都执行不到这里来
       return 0;
}
[root@localhost error_process]# gcc badptr.c 
[root@localhost error_process]# ./a.out 
a.out: badptr.c:14: main: Assertion `fp' failed.
  已放弃使用assert()的缺点是,频繁的调用会极大的影响程序的性能,增加额外的开销。在调试结束后,可以通过在包含#include 的语句之前插入 #define NDEBUG 来禁用assert调用,示例代码如下:
#include
#define NDEBUG
#include

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
/* 
函数: strcpy
原型: char * strcpy( char* _Dest, const char *_Source );
功能: 复制源串_Source到目标串_Dest所指定的位置, 
        包含NULL结束符. 不能处理源串与目标串重叠的情况. 
        函数返回_Dest值. 
*/ 
char * strcpy( char *_Dest, const char *_Source )
{
assert( ( NULL != _Dest ) && ( NULL != _Source ) );
 
char *_Temp = _Dest;
 
while ( ( *_Temp++ = *_Source++ ) != '/0' );
 
return _Dest;
}
 
/* 
函数: strlen的实现代码 
功能: size_t strlen( const char *_Str ); 
功能: 获取字符串长度, 字符串结束符NULL不计算在内. 
       没有返回值指示操作错误. 
*/ 
size_t strlen( const char *_Str )
{
assert( _Str != NULL );
 
size_t _Size = 0;
 
while ( *_Str++ )
{
_Size++;
}
 
return _Size;
}
 
/* 
函数: strcat
原型: char * strcat( char *_Dest, const char *_Source );
功能: 将源串_Src字符添加到目标串_Dst后. 
        本函数不能处理源串与目标串重叠的情况. 
*/ 
char * strcat( char *_Dest, const char *_Source )
{
assert( ( NULL != _Dest ) && ( NULL != _Source ) );
 
char *_Temp = _Dest;
 
while ( ( *++_Temp ) != NULL );
 
while ( ( *_Temp++ = *_Source++ ) != NULL );
return _Dest;
}
 
/* 
函数: strcmp
原型: int strcmp( const char *_Str1, const char *_Str2 ); 
功能: 比较字符串_Str1和_Str2大小. 
       返回值< 0, 表示_Str1小于_Str2; 
       返回值为0, 表示_Str1等于_Str2; 
       返回值> 0, 表示_Str1大于_Str2. 
*/ 
int strcmp( const char *_Str1, const char *_Str2 )
{
assert( ( NULL != _Str1 ) && ( NULL != _Str2 ) );
 
for ( ; *_Str1 == *_Str2; _Str1++, _Str2++ )
{
if ( ( '/0' == *_Str1 ) || ( '/0' == *_Str2 ) )
{
if ( *_Str1 == *_Str2 )
{
return 0;
}
else
{
break;
}
 
}
 
}
 
return ( ( *_Str1 > *_Str2 ) ? 1 : -1 );
}
 
/* 
函数: strlwr的实现代码 
功能: char * strlwr( char *_Str );
功能: 将字符串_Str的大写转换成小写并输出. 
*/ 
char * strlwr( char *_Str )
{
assert( NULL != _Str );
char *_Temp = _Str;
do
{
if ( ( *_Temp >= 'A' ) && ( *_Temp <= 'Z' ) )
{
*_Temp += 32;
}
}while( ( *_Temp++ ) != '/0' );
 
return _Str;
}
 
/* 
函数: strupr的实现代码 
功能: char * strupr( char *_Str );
功能: 将字符串_Str的小写转换成大写并输出. 
*/ 
char * strupr( char *_Str )
{
assert(  NULL != _Str);
char *_Temp = _Str;
do
{
if ( ( *_Temp >= 'a' ) && ( *_Temp <= 'z' ) )
{
*_Temp -= 32;
}
}while( ( *_Temp++ ) != '/0' );
 
return _Str;
}
 
/* 
函数: strrev的实现代码 
功能: char * strrev( char *_Str );
功能: 将字符串_Str倒转并输出. 
*/ 
char * strrev( char *_Str )
{
assert( NULL != _Str );
char *_Str1 = _Str;
char *_Str2 = _Str;
char _Temp;
while ( *_Str2 != '/0' )
{
*_Str2++;
}
 
if( _Str1 != _Str2 )
{
_Str2--;
}
 
while ( _Str1 < _Str2 )
{
_Temp = *_Str1;
*_Str1 = *_Str2;
*_Str2 = _Temp;
*_Str1++;
*_Str2--;
}
 
return _Str;
}
 
/* 
函数: strchr的实现代码 
功能: const char * strchr( const char *_Str, int _Val );
功能: 函数返回一个指向Str中_Val首次出现的位置,当没有在_Str中找_Val到返回NULL。
*/ 
const char * strchr( const char *_Str, int _Val )
{
assert( NULL != _Str );
for ( ; *_Str != (char)_Val; ++_Str )
{
if ( *_Str == '/0' )
{
return (const char *)NULL;
}
 
}
return _Str;
}
 
/* 
函数: strpbrk的实现代码 
功能: char * strpbrk( char *_Str, const char *_Control );
功能: 函数返回一个指针,它指向字符串_Control中任意字符在字符串_Str首次出现的位置,如果不存在返回NULL.
*/ 
char * strpbrk( char *_Str, const char *_Control )
{
assert( ( NULL != _Str ) && ( NULL != _Control ) );
char *_Str1;
const char *_Str2;
 
for ( _Str1 = _Str; *_Str1 != '/0'; ++_Str1 )
{
for ( _Str2 = _Control; *_Str2 != '/0'; ++_Str2 )
{
if ( *_Str1 == *_Str2 )
{
return _Str1;
}
 
}
 
}
 
return (char*)NULL;
}
 
/* 
函数: strstr的实现 
原型: char * strstr( char *_Str, const char *_SubStr ); 
功能: 在字符串_Str中查找_SubStr子串. 
       返回子串_SubStr在_Str中首次出现位置的指针. 
        如果没有找到子串_SubStr, 
        则返回NULL. 如果子串_SubStr为空串, 函数
        返回_Str值. 
*/ 
char * strstr( char *_Str, const char *_SubStr )
{
assert( ( NULL != _Str ) && ( NULL != _SubStr ) );
 
unsigned int i = 0;
if ( 0 == *_Str )
{
if ( *_SubStr )
{
return (char*)NULL;
}
 
return _Str;
}
 
while ( *_Str )
{
i = 0;
while (1)
{
if ( 0 == _SubStr[i] )
{
return _Str;
}
 
if ( _SubStr[i] != _Str[i] )
{
break;
}
 
i++;
}
 
_Str++;
}
return (char*)NULL;
}
 
/* 
函数: strtok的实现代码 
功能: char * strtok( char *_Str, const char *_Delim );
功能: 查找由_Delim指定分界符对_Str进行分解.首次调用_str指向要分解的字符串,以后调用_Str为NULL. 
*/ 
char * strtok( char *_Str, const char *_Delim )
{
static char *_Begin;
if ( NULL != _Str )
{
_Begin = strstr( _Str, _Delim );
while ( _Begin == _Str )
{
_Str += strlen( _Delim );
_Begin = strstr( _Str, _Delim );
}
 
}
else
{
_Str = _Begin;
if ( NULL == _Str )
{
return NULL;
}
 
_Begin = strstr( _Str, _Delim );
while ( _Begin == _Str )
{
_Str += strlen( _Delim );
_Begin = strstr( _Str, _Delim );
}
 
}
 
if ( NULL != _Begin )
{
memset( _Begin, 0, strlen( _Delim ) );
_Begin += strlen( _Delim );
}
else if ( 0 == *_Str )
{
return NULL;
}
 
return _Str;
}


阅读(1062) | 评论(0) | 转发(0) |
0

上一篇:makefile1

下一篇:oracle sql优化

给主人留下些什么吧!~~