分割字符串

来源:互联网 发布:网络架构图怎么做 编辑:程序博客网 时间:2024/06/10 04:41

1。strtok分割

#include "stdafx.h"#include "stdio.h"#include <iostream>#include <fstream>#include <string>#include <map>#include <vector>using namespace std;int _tmain(int argc, _TCHAR* argv[]){    ifstream in;    in.open("..\\train.txt");    string line;    vector<string> name;    vector<string> num;    if (in)    {        while (getline(in , line))        {            cout << line << endl;            char* str = new char[strlen(line.c_str()) + 1];            strcpy(str, line.c_str());            int i = 1;            char* tmp = strtok(str, " ");            while (tmp != NULL)            {                if (1 == i)                {                    name.push_back(tmp);                }                else                {                    num.push_back(tmp);                }                tmp = strtok(NULL, " ");                i = 2;            }            delete[] str;        }    }    else    {        cout << "File Error!" << endl;    }    in.close();    return 0;}

2.strrchr取最后一个分隔符的最后一个字符串

char copyName[1024];memset(copyName, 0, 1014);strcpy(copyName, registImgAllNames[Index[j]].c_str());char* frtpos = strrchr(copyName, '\/');char* frtpos1 = strrchr(copyName, '\\');if (frtpos < frtpos1)    frtpos = frtpos1;*frtpos = '\0';frtpos = strrchr(copyName, '\/');frtpos1 = strrchr(copyName, '\\');if (frtpos < frtpos1)    frtpos = frtpos1;
原创粉丝点击