Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/140.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/extjs/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 仅从字符串中提取整数_C++ - Fatal编程技术网

C++ 仅从字符串中提取整数

C++ 仅从字符串中提取整数,c++,C++,我想从某个字符串中提取整数,以便对其执行数学运算 例如字符串 25 1 3;5 9 2; 1 3 6 我只想提取 25,1,3,5,9,2,1,3,6 有什么方法可以做到这一点吗?有很多不同的方法可以做到这一点。例如,您可以使用并选择它们,但我认为从字符串流中提取将更容易 void extractIntegerWords(string str) { stringstream ss; /* Storing the whole string into string stream */

我想从某个字符串中提取整数,以便对其执行数学运算

例如字符串

25 1 3;5 9 2; 1 3 6

我只想提取

25,1,3,5,9,2,1,3,6


有什么方法可以做到这一点吗?

有很多不同的方法可以做到这一点。例如,您可以使用
并选择它们,但我认为从字符串流中提取将更容易

void extractIntegerWords(string str)
{
  stringstream ss;

  /* Storing the whole string into string stream */
  ss << str;

  /* Running loop till the end of the stream */
  string temp;
  int found;

  while (!ss.eof()) {

    /* extracting word by word from stream */
    ss >> temp;

    /* Checking the given word is integer or not */
    if (stringstream(temp) >> found)
        cout << found << " ";

    /* To save from space at the end of string */
    temp = "";
  }
}

// Driver code 
int main()
{
  string str = "25 1 3; 5 9 2; 1 3 6";
  extractIntegerWords(str);
  return 0;
}
void extracterintegerwords(字符串str)
{
细流ss;
/*将整个字符串存储到字符串流中*/
ss>temp;
/*检查给定单词是否为整数*/
如果(stringstream(temp)>>找到)

cout如果要从包含任何内容的字符串中提取它们(您不知道其他字符是什么),可以使用stringstream

例如,假设数字为int,并希望在列表中提取int:

注意,溶液“123 a12 13”将产生123和13,而不是123 12 13

若要从“123 a12 13”生成123 12 13,请在出现错误时读取字符而不是字符串:

#include <sstream>
#include <iostream>
#include <string>
#include <list>

int main(int argc, char ** argv)
{
  if (argc == 2) {
    std::stringstream iss(argv[1]);
    std::list<int> l;

    for (;;) {
      int v;

      if (iss >> v)
        l.push_back(v);
      else {
        iss.clear();

        char c;

        if (!(iss >> c))
          break;
      }
    }

    for (auto v : l)
      std::cout << v << ' ';
    std::cout << std::endl;
  }

  return 0;
}

一次性解决方案可以扫描字符串,查找一个范围,从下一个可用数字开始,在有非数字字符时停止。然后从该范围内的字符创建一个整数,并继续,直到到达字符串的结尾

向量提取(std::string const&str){ auto&ctype=std::use_facet(std::locale{}); 自动p1=str.data(),p2=str.data(); 向量v; 对于(自动e=&str.back()+1;p1
例如:

auto v=extract_ints(“[1,2,3]”);
对于(inti=0;i,我将使用空格、括号和分号作为分隔符来解析字符串

我以前回答过这些问题

我将该代码解释如下:

#include <strtk.hpp>   // http://www.partow.net/programming/strtk

std::string src = "[25 1 3; 5 9 2; 1 3 6]";

std::string delims(" [];");

std::vector<int> values;

strtk::parse(src, delims, values );

// values will contain all the integers in the string.
// if you want to get floats & integers the vector needs to be float
#包括//http://www.partow.net/programming/strtk
std::string src=“[25 1 3;5 9 2;1 3 6]”;
std::字符串delims(“[];”);
std::向量值;
strtk::parse(src、delims、value);
//值将包含字符串中的所有整数。
//如果你想得到浮点和整数,向量需要是浮点

是的,您可以使用正则表达式来选择由其他数字以外的数字分隔的数字。
#include <sstream>
#include <iostream>
#include <string>
#include <list>

int main(int argc, char ** argv)
{
  if (argc == 2) {
    std::stringstream iss(argv[1]);
    std::list<int> l;

    for (;;) {
      int v;

      if (iss >> v)
        l.push_back(v);
      else {
        iss.clear();

        char c;

        if (!(iss >> c))
          break;
      }
    }

    for (auto v : l)
      std::cout << v << ' ';
    std::cout << std::endl;
  }

  return 0;
}
pi@raspberrypi:~ $ g++ -pedantic -Wall -Wextra c.cc
pi@raspberrypi:~ $ ./a.out "25 1 3; 5 9 2; 1 3 6"
25 1 3 5 9 2 1 3 6 
pi@raspberrypi:~ $ ./a.out "123 a12 13"
123 12 13 
#include <strtk.hpp>   // http://www.partow.net/programming/strtk

std::string src = "[25 1 3; 5 9 2; 1 3 6]";

std::string delims(" [];");

std::vector<int> values;

strtk::parse(src, delims, values );

// values will contain all the integers in the string.
// if you want to get floats & integers the vector needs to be float