• C++字符串分割


    C++

    通过案例进行使用和学习
    

    代码示例

     #include<iostream>
     #include<string>
     #include<vector>
     using namespace std;
     
     vector<string> split(const string& str, const string& delim) {  
     	vector<string> res;  
     	if("" == str) return res;  
     	//先将要切割的字符串从string类型转换为char*类型  
     	char * strs = new char[str.length() + 1] ; //不要忘了  
     	strcpy(strs, str.c_str());   
      
     	char * d = new char[delim.length() + 1];  
     	strcpy(d, delim.c_str());  
      
     	char *p = strtok(strs, d);  
     	while(p) {  
     		string s = p; //分割得到的字符串转换为string类型  
     		res.push_back(s); //存入结果数组  
     		p = strtok(NULL, d);  
     	}  
      
     	return res;  
     	// 返回一个 vector<string>类的容器
     }  
     
      int main(int argc,char**argv)
      {
         string str1 = "how are you?";
     	vector<string> elm = split(str1," ");
     	for(int i = 0; i < elm.size(); i++){
     		cout<<elm[i];
     	}
          return 0;
      }
    
    说明:
       当返回值是一个数组时,不妨采用vector类  用vector和string容器实现
       C 库函数 char *strtok(char *str, const char *delim) 分解字符串 str 为一组字符串,delim 为分隔符  strtok(  )函数包含于头文件 string.h
                                                          NULL的作用只是为了使得每次调用时,都不是从"My name is XiaoMing."的头开始,而是从上次调用时查找所停止的位置开始
                char *strcpy(char *dest,const char *src)    把 src 所指向的字符串复制到 dest
    			
    	标准库的string类提供了3个成员函数来从一个string得到c类型的字符数组:c_str()、data()、copy(p,n)
    	在C语言中没有string类型,故必须通过string类对象的成员函数c_str()把string对象转换成C中的字符串样式
    	一定要使用strcpy()等函数来操作c_str()返回的指针
    	
       & 读作引用   // 声明引用变量  引用通常用于函数参数列表和函数返回
          使用指针 来实现引用调用函数
    	  使用引用 来实现引用调用函数。
     C++头文件中 
     #include<iostream> 
     #include<fstream>    ifstream  ofstream
     #include<sstream>
     
     
    #include<cstring>//字符串
        memset strcat strcmp strcpy strlen strncat strncmpf strncpy strstr 
    #include <string> 
     
    #include<cstdio>   //<cstdio> 为标准流提供C样式的输入和输出 
        使用FILE输出txt文件与fopen格式  fopen  fprintf  fclose
    	  C 文件打开 
    	   头文件 : stdio.h​
           原型 : FILE* fopen ( const char* path , const char* mode ) ;​
    	  C 函数 fprintf 写出文件
    	    头文件 : stdio.h
    		原型 : ​int fprintf (FILE* stream, const char*format, [argument])​​​
    示例
     //1. fprintf 写出文本
    
      //创建一个只写的文件 , 每次执行都重新写入内容
      //  这里使用相对路径 , 直接在代码路径下创建一个文件
      FILE* file_text_write = fopen("my_file.txt", "w");
    
      //文本形式写出内容 : 使用格式化的方式输出文本内容
      fprintf(file_text_write, "姓名:%s 学号:%d", "Tom", 1);
    
      //关闭文件 , 只有有 fopen , 就必须有对应的 fclose 与之对应
      fclose(file_text_write);
     
     
    读写
    读 
       fscanf 读取文件 
       fgets 
       fgetc    fputc
       fread()  fwrite()
    写
    
    C++ 语法
      如c++中的vector头文件里面就有这个push_back函数,在vector类中作用为在vector尾部加入一个数据。
    
    用 C/C++ 遍历目录文件主要有两种方式
    目录文件 分别对应在 Windows VS 环境下和 Linux\Unix 环境下的方法,
      它们各自所使用的函数如下:
     (Windows VS)_findfirst, _findnext, _findclose
      ( Linux\Unix)opendir, readdir, closedir    包含头文件 <sys/types.h> 和 <dirent.h>
         基本流程:opendir-->readdir-->closedir
    	   DIR *opendir(const char *pathname);  // 首先要打开目录,也就是要调用函数 opendir--
    	         dp 为调用 opendir 时的返回值。当读取成功时,函数返回一个保存文件信息的 dirent 结构体指针
    	   在 <dirent.h> 中定义了一个结构体 dirent ,用来保存文件信息,   d_type 和 d_name 这两个字段,分别表示文件类型和文件名
    	   
    文本
      头文件中 #include<iostream> #include<fstream>  #include<sstream>
       文本读取 ifstream
       文本写入 ofstream 类,包含在 fstream 
       
    使用FILE输出txt文件与fopen格式  fopen  fprintf  fclose
    

    解析参数

      GNU C提供的函数getopt、getopt_long、getopt_long_only函数来解析命令行参数 
           命令行参数可以分为两类,一类是短选项,一类是长选项,
    	     短选项在参数前加一杠"-",
    		 长选项在参数前连续加两杠"--"
       argc 是 argument count的缩写,表示传入main函数的参数个数;
    
       argv 是 argument vector的缩写,表示传入main函数的参数序列或指针,
       并且第一个参数argv[0]一定是程序的名称,并且包含了程序所在的完整路径,
       所以确切的说需要我们输入的main函数的参数个数应该是argc-1个	 argv是指向指针的指针 
       一般编译器默认使用argc和argv两个名称作为main函数的参数,但这两个参数如此命名并不是必须的,你可以使用任何符合C++语言命名规范的变量名作为入参
    		 
       include <getopt.h>
       getopt   的参数包括argc及argv,它的功能是解析可选参数(以-开头的参数) getopt函数只能处理短选项
       getopt_long  argc和argv 且多了两个参数 longopts 及 longindex  getopt_long函数两者都可以
        int getopt_long(int argc, char * const argv[], const char *optstring, const struct option *longopts, int *longindex);
    
    	struct option
         {
            const char *name;
            int has_arg;
            int *flag;
            int val;
         };
    
        static struct option long_options[] = {{"help", no_argument, 0, 'h'},  {"task", required_argument, 0, 't'}, {"cestest", no_argument, 0, 'c'},  {nullptr, 0, nullptr, 0}};
        struct Args
        {
            bool help{false};
            std::string task;
            bool cestest{false};
        };
    	// cd build && ./aidata --task tes--cestest
    	args.task == "cestest"    
       
        warning: format '%s' expects argument of type 'char*', but argument 2 has type 'std::__cxx11::string {aka std::__cxx11::basic_string<char>}' [-Wformat=]
    

    参考:

     https://blog.csdn.net/weixin_47826078/article/details/120444666 C++实现按指定子串分割母串(split)函数 按空格分割string字符串
     https://www.cnblogs.com/cyblogs/p/9102854.html C/C++ 遍历目录文件,默认目录下(转载)
     https://blog.51cto.com/u_14202100/5084913#V_C__fputc__202
     C++获取指定路径文件夹下的所有图片-windows和linux下各自的处理方案  https://blog.csdn.net/a2824256/article/details/112302856
  • 相关阅读:
    selenium-使用xpath结合text内容定位元素
    pip 安装第三方库报错解决方法
    本地使用git拉取远程指定分支代码
    Python之字符串格式化(format)
    Python之字符串format格式化
    Python中的eval()函数
    用PIL实现滤镜(一)——素描、铅笔画效果
    python勾画图片轮廓
    用python读取带密码的excel文件中的数据
    python中的列表与列表推导式
  • 原文地址:https://www.cnblogs.com/ytwang/p/16224692.html
Copyright © 2020-2023  润新知