如何在整数数组(c+;+;)中拆分输入字符串 我刚开始学习C++。 在Java中,要分割输入,只需使用split方法分割输入中的空格。 有没有其他简单的方法可以将字符串输入拆分为整数数组? 我不在乎效率;我只想要一些代码,可以帮助我理解如何从输入中分割空格

如何在整数数组(c+;+;)中拆分输入字符串 我刚开始学习C++。 在Java中,要分割输入,只需使用split方法分割输入中的空格。 有没有其他简单的方法可以将字符串输入拆分为整数数组? 我不在乎效率;我只想要一些代码,可以帮助我理解如何从输入中分割空格,c++,arrays,string,C++,Arrays,String,例如: 输入:1234 代码: #包括 #包括 int main() { 国际清单[4]; 对于(int i=0;i>list[i]; } std::cout#包括 #包括 int main() { 国际清单[4]; 对于(int i=0;i>list[i]; } STD::C++中的CUT,这也可以用基本上单个函数调用来处理。 例如: std::string input = "1 2 3 4"; // The string we should "split" std::vector<

例如: 输入:1234 代码:

#包括
#包括
int main()
{
国际清单[4];
对于(int i=0;i>list[i];
}
std::cout
#包括
#包括
int main()
{
国际清单[4];
对于(int i=0;i>list[i];
}

STD::C++中的CUT

,这也可以用基本上单个函数调用来处理。 例如:

std::string input = "1 2 3 4";  // The string we should "split"

std::vector<int> output;  // Vector to contain the results of the "split"

std::istringstream istr(input);  // Temporary string stream to "read" from

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

在C++中,这也可以用基本上单个函数调用来处理。 例如:

std::string input = "1 2 3 4";  // The string we should "split"

std::vector<int> output;  // Vector to contain the results of the "split"

std::istringstream istr(input);  // Temporary string stream to "read" from

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

@ EricChoi如果你不知道代码> STD::CIN < /Cord>,那么立即停止!启动OC.CIN是从C++中获得终端输入的基本方法。它进入头,而STD::除非你使用“命名空间STD”;在程序顶部。使用cin>>x;将从终端获取输入,直到下一个空格,并尝试将该字符流转换为任何类型的x。如果无法使conversion@EricChoi如果您不知道std::cin
,请立即停止!重新开始。cin是在C++中从终端获取输入,它出现在头文件中,而STD::除非你使用“命名空间STD”;在程序顶部。使用cin>>x;将从终端获取输入,直到下一个空格,并尝试将该字符流转换为任何类型的x。如果无法使转换成为可能的重复,则会引发异常
std::string input = "1 2 3 4";  // The string we should "split"

std::vector<int> output;  // Vector to contain the results of the "split"

std::istringstream istr(input);  // Temporary string stream to "read" from

std::copy(std::istream_iterator<int>(istr),
          std::istream_iterator<int>(),
          std::back_inserter(output));
std::vector<int> output;  // Vector to contain the results of the "split"

std::copy(std::istream_iterator<int>(std::cin),
          std::istream_iterator<int>(),
          std::back_inserter(output));