简体   繁体   English

用C ++标记字符串

[英]Tokenize a String in C++

I have a string currentLine="12 23 45" 我有一个字符串currentLine =“12 23 45”

I need to extract 12, 23, 45 from this string without using Boost libraries. 我需要从这个字符串中提取12,23,45而不使用Boost库。 Since i am using string, strtok fails for me. 由于我使用字符串,strtok对我失败了。 I have tried a number of things still no success. 我尝试了很多事情仍然没有成功。

Here is my last attempt 这是我的最后一次尝试

while(!inputFile.eof())
    while(getline(inputFile,currentLine))
        {
            int countVar=0;
            int inputArray[10];
            char* tokStr;
            tokStr=(char*)strtok(currentLine.c_str()," ");

            while(tokstr!=NULL)
            {
            inputArray[countVar]=(int)tokstr;
            countVar++;
            tokstr=strtok(NULL," ");
            }
        }
}

the one without strtok 没有strtok的人

string currentLine;
while(!inputFile.eof())
    while(getline(inputFile,currentLine))
        {
            cout<<atoi(currentLine.c_str())<<" "<<endl;
            int b=0,c=0;
            for(int i=1;i<currentLine.length();i++)
                {
                    bool lockOpen=false;
                    if((currentLine[i]==' ') && (lockOpen==false))
                        {
                        b=i;
                        lockOpen=true;
                        continue;
                        }
                    if((currentLine[i]==' ') && (lockOpen==true))
                        {
                        c=i;
                        break;
                        }
                }
            cout<<b<<"b is"<<" "<<c;    
        }

Try this: 尝试这个:

#include <sstream>

std::string str = "12 34 56";
int a,b,c;

std::istringstream stream(str);
stream >> a >> b >> c;

Read a lot about c++ streams here: http://www.cplusplus.com/reference/iostream/ 在这里阅读很多关于c ++流的内容: http//www.cplusplus.com/reference/iostream/

std::istringstream istr(your_string);

std::vector<int> numbers;
int number;
while (istr >> number)
    numbers.push_back(number);

Or, simpler (though not really shorter): 或者,更简单(虽然不是很短):

std::vector<int> numbers;
std::copy(
    std::istream_iterator<int>(istr),
    std::istream_iterator<int>(),
    std::back_inserter(numbers));

(Requires the standard headers <sstream> , <algorithm> and <iterator> .) (需要标准头<sstream><algorithm><iterator> 。)

You can also opt for Boost tokenizer ...... 你也可以选择Boost标记器......

#include <iostream>
#include <string>
#include <boost/foreach.hpp>
#include <boost/tokenizer.hpp>
using namespace std;
using namespace boost;

int main(int argc, char** argv)
{
    string str= "India, gold   was dear";
    char_separator<char> sep(", ");
    tokenizer< char_separator<char> > tokens(str, sep);
    BOOST_FOREACH(string t, tokens)
    {
        cout << t << "." << endl;
    }
}

stringstream and boost::tokenizer are two possibilities. stringstreamboost::tokenizer是两种可能性。 Here is a more explicit solution using string::find and string::substr . 这是使用string::findstring::substr的更明确的解决方案。

std::list<std::string>
tokenize(
  std::string const& str,
  char const token[])
{
  std::list<std::string> results;
  std::string::size_type j = 0;
  while (j < str.length())
  {
    std::string::size_type k = str.find(token, j);
    if (k == std::string::npos)
      k = str.length();

    results.push_back(str.substr(j, k-j));
    j = k + 1;
  }
  return results;
}

Hope this helps. 希望这可以帮助。 You can easily turn this into an algorithm that writes the tokens to arbitrary containers or takes a function handle that processes the tokens. 您可以轻松地将其转换为将令牌写入任意容器的算法,或者使用处理令牌的函数句柄。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM