将字符串拆分为 C++ 中的字符数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2158943/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:19:36  来源:igfitidea点击:

Split string into array of chars in C++

c++stringsplit

提问by Galileo

I'm writing a program that requires a string to be inputed, then broken up into individual letters. Essentially, I need help finding a way to turn "string" into ["s","t","r","i","n","g"]. The strings are also stored using the string data type instead of just an array of chars be default. I would like to keep it that way and avoid char, but will use it if necessary.

我正在编写一个程序,需要输入一个字符串,然后分解成单个字母。本质上,我需要帮助找到一种将“字符串”转换为 ["s","t","r","i","n","g"] 的方法。字符串也使用字符串数据类型存储,而不仅仅是默认的字符数组。我想保持这种方式并避免使用字符,但会在必要时使用它。

Any help would be nice, thank you in advance.

任何帮助都会很好,提前致谢。

回答by Ryan

Assuming you already have the string inputted:

假设您已经输入了字符串:

string s("string");
vector<char> v(s.begin(), s.end());

This will fill the vector vwith the characters from a string.

这将v用字符串中的字符填充向量。

回答by Luca Matteis

string a = "hello"; 
cout << a[1];

I hope that explains it

我希望能解释它

回答by R Samuel Klatchko

A string is just a sequence of the underlying character (i.e. char for std::string and wchar_t for std::wstring).

字符串只是底层字符的序列(即 std::string 的 char 和 std::wstring 的 wchar_t )。

Because of that, you easily get each letter:

因此,您可以轻松获得每个字母:

for (std::string::size_type l = 0; l < str.length(); ++l)
{
    std::string::value_type c = str[l];
}

回答by Thomas Matthews

Try using the c_str()method of std::string:

尝试使用以下c_str()方法std::string

#include <string>
using namespace std;

int main(void)
{
  string text = "hello";
  size_t length = text.length() + sizeof('
string input ="some string for example my cat is handsome";
vector<string> split_char_to_vector(string input) {
  vector<string> output;
  for(size_t i=0;i<=input.length();i++) {
    output.push_back(input[i]));
  }
  return output;
}
'); char * letters = new char[length]; strcpy(letters, length.c_str()); for (unsigned int i = 0; i < length; ++i) { cout << '[' << i << "] == '" << letters[i] << "'\n"; } return EXIT_SUCCESS; }

回答by Thomas Matthews

 char array[1000];
std::string input="i dont think love never ends";
 for(size_t i=0;i<=input.length();/*or string::npos;*/i++)
  {
            if (input[i] != '
  char array[1000];
   std::string input="i dont think love never ends";
   for(size_t i=0;i<=input.length();/*or string::npos;*/i++)
    {
        if (input[i] != '##代码##')
        {
        array[i] = input[i];
        }
    else
        {
        break;
        }
    }
    //to avoid noise after position input.length();
        for (size_t i = input.length(); i <= 1000; i++)
        {
             array[i] = '##代码##';
        }
      //ie return array; or print
    for (size_t i = 0; i < 100; i++)
    {
    std::cout << array[i] << std::endl;
    }
') { array[i] = input[i]; } else { break; } } for (size_t i = 0; i < 100; i++) { std::cout << array[i] << std::endl; }

if you want to convert split strings into character the first traverse the string and write for each characters of string to the i'th position of char array ie

如果要将拆分的字符串转换为字符,则首先遍历字符串并将字符串的每个字符写入字符数组的第 i 个位置,即

##代码##

回答by Thomas Matthews

if you want to convert split strings into character the first traverse the string and write for each characters of string to the i'th position of char array ie

如果要将拆分的字符串转换为字符,则首先遍历字符串并将字符串的每个字符写入字符数组的第 i 个位置,即

##代码##