2017-05-19 44 views
-4

我想将一个带空格的单个字符串拆分为三个单独的字符串。例如,我有一个字符串(str1)。用户输入任何3个单词,如 "Hey it's me""It's hot out"。 从那里,我需要写一个函数,将采取此字符串(str1)并将其分成三个不同的字符串。这样(以第一个例子),那么它会说:在空格处拆分一个句子(字符串)

Hey (is the first part of the string) 
it's (is the second part of the string) 
me (is the third part of the string) 

我有我应该使用拆分在空间中的字符串,操作难度。

这是我的代码到目前为止,这仅仅是用户将如何进入input.I正在寻找做到这一点使用istringstream没有最基本的方法!只使用基本的字符串操作,如find(),substr()。

**我期待创建一个单独的功能执行字符串的分手**我想出如何使用此代码获取输入的第一部分:

cout << "Enter a string" << endl; 
getline(cin, one); 

position = str1.find(' ', position); 
first_section = str1.substr(0, position); 

但现在我不知道如何将字符串的第二部分或第三部分分成自己的字符串。我正在考虑一个for循环可能?不确定。


#include <iostream> 
#include <string> 
using namespace std; 

int main() { 
    string str1; 
    cout << "Enter three words: "; 
    getline(cin, str1); 
    while(cin) { 
     cout << "Original string: " << str1 << endl; 
     cin >> str1; 
    } 

    return; 
} 
+0

你可以考虑使用C函数去做?例如。 'strtok'? – Elyasin

+0

我只严格使用C++ – Alyssa

+0

关于如何在C++中分割字符串有很多帖子,最简单的一个不需要你编写专门的函数来完成它。那样就是使用'std :: istringstream'。 – PaulMcKenzie

回答

0

我有我应该使用拆分在空间中的字符串,操作难度。

  1. str1使用std::istringstream
  2. std::istringstream中读取每个令牌。

// No need to use a while loop unless you wish to do the same 
// thing for multiple lines. 
// while(cin) { 

    cout << "Original string: " << str1 << endl; 
    std::istringstream stream(str1); 
    std::string token1; 
    std::string token2; 
    std::string token3; 

    stream >> token1 >> token2 >> token3; 

    // Use the tokens anyway you wish 
// } 

如果你希望做同样的事情的投入,使用多行:

int main() { 
    string str1; 
    cout << "Enter three words: "; 
    while(getline(cin, str1)) 
    { 
     cout << "Original string: " << str1 << endl; 
     std::istringstream stream(str1); 
     std::string token1; 
     std::string token2; 
     std::string token3; 

     stream >> token1 >> token2 >> token3; 

     // Use the tokens anyway you wish 

     // Prompt the user for another line 
     cout << "Enter three words: "; 
    } 
} 
0

也许最根本的解决方案是使用驻留在你的循环内阅读一个单词。例如:

cin >> word1;  // extracts the first word 
cin >> word2;  // extracts the second word 
getline(cin, line); // extracts the rest of the line 

可以使用结果返回值这些表达式的检查成功:

#include <string> 
#include <iostream> 

int main(void) { 
    std::string word1, word2, line; 
    int success = std::cin >> word1 && std::cin >> word2 
            && !!std::getline(std::cin, line); // double-! necessary? 
    if (success) { std::cout << "GOOD NEWS!" << std::endl; } 
    else   { std::cout << "bad news :(" << std::endl; } 
    return 0; 
} 

或者,在这样的字符串我希望两个空格。我的建议是使用string::find找到像这样的第一和第二位:

size_t first_position = str1.find(' ', 0); 

你或许应该检查这对string::npos为契机,以处理错误。以下是:

size_t second_position = str1.find(' ', first_position + 1); 

下一页错误处理检查,并在此之后,它应该然后是微不足道的使用string::substr拆分该字符串段像这样:

string first_section = str1.substr(0    , first_position) 
    , second_section = str1.substr(first_position , second_position) 
    , third_section = str1.substr(second_position, string::npos); 
+0

由于某些原因,当我去存储first_position在first_section字符串中,我得到一个错误,说:[错误]没有匹配函数调用'std :: basic_string :: substr(int,std :: string&)' – Alyssa

+0

@Alyssa你介意提供一些代码来证明这个问题吗?好像你在调用类似'str1.substr(0,some_string);''some_string'是错误的原因;你需要提供一个*整数*('size_t',特别是)值。但这只是一种猜测,如果没有您遇到问题的代码,我无法验证或无效。 – Sebivor

0

我有了一本实用工具类一堆字符串操作的方法。我将显示用分隔符分割字符串的类函数。这个类有私有构造函数,所以你不能创建这个类的实例。所有的方法都是静态方法。

Utility.h

#ifndef UTILITY_H 
#define UTILITY_h 

// Library Includes Here: vector, string etc. 

class Utility { 
public: 
    static std::vector<std::string> splitString(const std::string& strStringToSplit, 
               const std::string& strDelimiter, 
               const bool keepEmpty = true); 

private: 
    Utility(); 
}; 

Utility.cpp

std::vector<std::string> Utility::splitString(const std::string& strStringToSplit, 
               const std::string& strDelimiter, 
               const bool keepEmpty) { 
    std::vector<std::string> vResult; 
    if (strDelimiter.empty()) { 
     vResult.push_back(strStringToSplit); 
     return vResult; 
    } 

    std::string::const_iterator itSubStrStart = strStringToSplit.begin(), itSubStrEnd; 
    while (true) { 
     itSubStrEnd = search(itSubStrStart, strStringToSplit.end(), strDelimiter.begin(), strDelimiter.end()); 
     std::string strTemp(itSubStrStart, itSubStrEnd); 
     if (keepEmpty || !strTemp.empty()) { 
      vResult.push_back(strTemp); 
     } 

     if (itSubStrEnd == strStringToSplit.end()) { 
      break; 
     } 

     itSubStrStart = itSubStrEnd + strDelimiter.size(); 
    } 

    return vResult;  
} 

Main.cpp的 - 使用

#include <string> 
#include <vector> 
#include "Utility.h" 

int main() { 
    std::string myString("Hello World How Are You Today"); 

    std::vector<std::string> vStrings = Utility::splitString(myString, " "); 

    // Check Vector Of Strings 
    for (unsigned n = 0; n < vStrings.size(); ++n) { 
     std::cout << vStrings[n] << " "; 
    } 
    std::cout << std::endl; 

    // The Delimiter is also not restricted to just a single character 
    std::string myString2("Hello, World, How, Are, You, Today"); 

    // Clear Out Vector 
    vStrings.clear(); 

    vStrings = Utility::splitString(myString2, ", "); // Delimiter = Comma & Space 

    // Test Vector Again 
    for (unsigned n = 0; n < vStrings.size(); ++n) { 
     std::cout << vStrings[n] << " "; 
    } 
    std::cout << std::endl; 

    return 0; 
} 
相关问题