📜  在C++中标记字符串

📅  最后修改于: 2021-05-25 21:41:31             🧑  作者: Mango

标记字符串表示相对于某些定界符分割字符串。有很多标记字符串。在本文中,将对其中的四个进行说明:

使用stringstream

一个字符串流与允许你从字符串,就好像它是一个流中读取流的字符串对象关联。
下面是C++的实现:

C++
// Tokenizing a string using stringstream
#include 
  
using namespace std;
  
int main()
{
      
    string line = "GeeksForGeeks is a must try";
      
    // Vector of string to save tokens
    vector  tokens;
      
    // stringstream class check1
    stringstream check1(line);
      
    string intermediate;
      
    // Tokenizing w.r.t. space ' '
    while(getline(check1, intermediate, ' '))
    {
        tokens.push_back(intermediate);
    }
      
    // Printing the token vector
    for(int i = 0; i < tokens.size(); i++)
        cout << tokens[i] << '\n';
}


C++
// C/C++ program for splitting a string
// using strtok()
#include 
#include 
  
int main()
{
    char str[] = "Geeks-for-Geeks";
  
    // Returns first token 
    char *token = strtok(str, "-");
  
    // Keep printing tokens while one of the
    // delimiters present in str[].
    while (token != NULL)
    {
        printf("%s\n", token);
        token = strtok(NULL, "-");
    }
  
    return 0;
}


C
// C code to demonstrate working of
// strtok
#include 
#include 
  
// Driver function
int main()
{
 // Declaration of string
    char gfg[100] = " Geeks - for - geeks - Contribute";
  
    // Declaration of delimiter
    const char s[4] = "-";
    char* tok;
  
    // Use of strtok
    // get first token
    tok = strtok(gfg, s);
  
    // Checks for delimeter
    while (tok != 0) {
        printf(" %s\n", tok);
  
        // Use of strtok
        // go through other tokens
        tok = strtok(0, s);
    }
  
    return (0);
}


CPP
// C/C++ program to demonstrate working of strtok_r()
// by splitting string based on space character.
#include
#include
  
int main()
{
    char str[] = "Geeks for Geeks";
    char *token;
    char *rest = str;
  
    while ((token = strtok_r(rest, " ", &rest)))
        printf("%s\n", token);
  
    return(0);
}


C++
// CPP program for above approach
#include 
#include 
#include 
#include 
  
/**
 * @brief Tokenize the given vector 
   according to the regex
 * and remove the empty tokens.
 *
 * @param str
 * @param re
 * @return std::vector
 */
std::vector tokenize(
                     const std::string str,
                          const std::regex re)
{
    std::sregex_token_iterator it{ str.begin(), 
                             str.end(), re, -1 };
    std::vector tokenized{ it, {} };
  
    // Additional check to remove empty strings
    tokenized.erase(
        std::remove_if(tokenized.begin(), 
                            tokenized.end(),
                       [](std::string const& s) {
                           return s.size() == 0;
                       }),
        tokenized.end());
  
    return tokenized;
}
  
// Driver Code
int main()
{
    const std::string str = "Break string 
                   a,spaces,and,commas";
    const std::regex re(R"([\s|,]+)");
    
    // Function Call
    const std::vector tokenized = 
                           tokenize(str, re);
    
    for (std::string token : tokenized)
        std::cout << token << std::endl;
    return 0;
}


输出
GeeksForGeeks
is
a
must
try


使用strtok()

// Splits str[] according to given delimiters.
// and returns next token. It needs to be called
// in a loop to get all tokens. It returns NULL
// when there are no more tokens.
char * strtok(char str[], const char *delims);


下面是C++的实现:

C++

// C/C++ program for splitting a string
// using strtok()
#include 
#include 
  
int main()
{
    char str[] = "Geeks-for-Geeks";
  
    // Returns first token 
    char *token = strtok(str, "-");
  
    // Keep printing tokens while one of the
    // delimiters present in str[].
    while (token != NULL)
    {
        printf("%s\n", token);
        token = strtok(NULL, "-");
    }
  
    return 0;
}
输出
Geeks
for
Geeks


strtok()的另一个示例:

C

// C code to demonstrate working of
// strtok
#include 
#include 
  
// Driver function
int main()
{
 // Declaration of string
    char gfg[100] = " Geeks - for - geeks - Contribute";
  
    // Declaration of delimiter
    const char s[4] = "-";
    char* tok;
  
    // Use of strtok
    // get first token
    tok = strtok(gfg, s);
  
    // Checks for delimeter
    while (tok != 0) {
        printf(" %s\n", tok);
  
        // Use of strtok
        // go through other tokens
        tok = strtok(0, s);
    }
  
    return (0);
}
输出
Geeks 
  for 
  geeks 
  Contribute


使用strtok_r()

就像C语言中的strtok()函数一样, strtok_r()执行将字符串解析为标记序列的相同任务。 strtok_r()是strtok()的可重入版本。
有两种方法可以调用strtok_r()

// The third argument saveptr is a pointer to a char * 
// variable that is used internally by strtok_r() in 
// order to maintain context between successive calls
// that parse the same string.
char *strtok_r(char *str, const char *delim, char **saveptr);


下面是一个简单的C++程序,用于显示strtok_r()的用法:

CPP

// C/C++ program to demonstrate working of strtok_r()
// by splitting string based on space character.
#include
#include
  
int main()
{
    char str[] = "Geeks for Geeks";
    char *token;
    char *rest = str;
  
    while ((token = strtok_r(rest, " ", &rest)))
        printf("%s\n", token);
  
    return(0);
}
输出
Geeks
for
Geeks


使用std :: sregex_token_iterator

在这种方法中,基于正则表达式匹配进行标记化。当需要多个定界符时,更适合用例。

下面是一个简单的C++程序,用于显示std :: sregex_token_iterator的用法:

C++

// CPP program for above approach
#include 
#include 
#include 
#include 
  
/**
 * @brief Tokenize the given vector 
   according to the regex
 * and remove the empty tokens.
 *
 * @param str
 * @param re
 * @return std::vector
 */
std::vector tokenize(
                     const std::string str,
                          const std::regex re)
{
    std::sregex_token_iterator it{ str.begin(), 
                             str.end(), re, -1 };
    std::vector tokenized{ it, {} };
  
    // Additional check to remove empty strings
    tokenized.erase(
        std::remove_if(tokenized.begin(), 
                            tokenized.end(),
                       [](std::string const& s) {
                           return s.size() == 0;
                       }),
        tokenized.end());
  
    return tokenized;
}
  
// Driver Code
int main()
{
    const std::string str = "Break string 
                   a,spaces,and,commas";
    const std::regex re(R"([\s|,]+)");
    
    // Function Call
    const std::vector tokenized = 
                           tokenize(str, re);
    
    for (std::string token : tokenized)
        std::cout << token << std::endl;
    return 0;
}
输出
Break
string
a
spaces
and
commas
想要从精选的最佳视频中学习和练习问题,请查看《基础知识到高级C的C基础课程》。