我需要创建一个函数来获取字符串中的这些数字

I need to create a function that gets those numbers in the string

本文关键字:字符串 获取 数字 函数 一个 创建      更新时间:2023-10-16

我正在尝试使一个接受参数的函数应该是这样的

string s1 = "[1 -2.5 3;4 5.25 6;7 8 9.12]";

它应该只返回数字,没有分号或 [ 和 ] 和空格

喜欢这个:

1 -2.5 3 4 5.25 6 7 8 9.12 

所以我稍后可以将字符串转换为浮点数并将它们保存到数组中

有什么想法吗?

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

string s1 = "[1 -2.5 3;4 5.25 6;7 8 9.12]";
void cutter(string s){

    for(int i=1;i<s.length();i++){
        if(i != s.find(" ") &&  i != s.find(";") ){
            cout << s[i];
            s.erase(0,i-1);
        }
        else if(i == s.find(" ") || i == s.find(";") ){
            cout<<endl;
        }

    }
}
int main()
{
    cutter(s1);
    return 0;
}

要删除[]并将;替换为空格,您可以执行以下操作:

#include <algorithm>
void cutter(string &s) {
    s.erase(std::remove(s.begin(), s.end(), '['), s.end());
    s.erase(std::remove(s.begin(), s.end(), ']'), s.end());
    std::replace(s.begin(), s.end(), ';', ' ');
}

无需对字符串进行任何手动循环。还要注意,在函数的签名中,需要string &s,而不是string s。如果没有该&则将字符串的副本传递给函数,并且该副本将在末尾被丢弃,从而导致对原始副本没有任何更改。使用&,您可以传递引用,程序将按预期工作。


当然,您也可以通过副本传递它,而是返回修改后的字符串:

std::string cutter(std::string s) {
    // modify the string here
    return s; // returns the modified copy
}

main中,执行以下操作:

s1 = cutter(s1); // assign the result to the original to change it

如果你有提升,那么这几乎是微不足道的:

std::vector<std::string> result;
boost::remove_erase_if(s1, boost::is_any_of("[]"));
boost::split(result, s1, boost::is_any_of(" ;"));

不过,您可能需要获取 S1 的副本,因为在本例中它将发生突变。

这是一个

基于正则表达式的解决方案(C++11 及更高版本(:

#include <iostream>
#include <regex>
using namespace std;
string cutter(string src) {
 string out, dlm;
 regex e(R"(-?d+.?d*)");
 smatch m;
 while(regex_search(src,m,e)) {
  for(const auto &x: m) 
   { out += dlm + string(x); dlm = ' '; }
  src = m.suffix().str();
 }
 return out;
}
int main()
{
 string s1 = "[1 -2.5 3;4 5.25 6;7 8 9.12]";
 cout << cutter(move(s1)) << endl;   
 return 0;
}

输出:

1 -2.5 3 4 5.25 6 7 8 9.12

另外,基于删除额外字符的解决方案(但第一个习惯性更可取(:

#include <iostream>
#include <regex>
using namespace std;
string cutter(const string &src) {
 regex e("[ ;]+");
 string out = std::regex_replace(src,e," ");
 return out.substr(1, out.size()-2);
}
int main()
{
 string s1 = "[1 -2.5 3;4 5.25 6;7 8 9.12]";
 cout << cutter(s1) << endl;   
 return 0;
}

下面是使用两个函数的解决方案。

一个

函数返回一个不带 [、] 和 ;

另一个函数将具有浮点值的字符串转换为浮点数向量。

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
std::string s1 = "[1 -2.5 3;4 5.25 6;7 8 9.12]";
std::string cutter(std::string &s){
    std::string res = "";
    for (auto c : s)                                 // Loop over all chars in s
    {
        if (c == ';') res += ' ';                    // Replace ; with space
        else if ((c != '[') && (c != ']')) res += c; // Skip [ and ]
    }
    return res;
}
std::vector<float> string_to_floats(std::string &s)
{
      float f;
      std::vector<float> res;
      std::stringstream stream(s);        // Create and initialize the stream
      while(1)
      {
          stream >> f;                    // Try to read a float
          if (stream.fail()) return res;  // If it failed, return the result
          res.push_back(f);               // Save the float in the result vector
      }
}
int main()
{
    std::string s2 = cutter(s1);
    std::cout << s2 << std::endl;
    std::vector<float> values = string_to_floats(s2);
    std::cout << "Number of floats: " << values.size() << std::endl;
    for (auto f : values) std::cout << f << std::endl;
    return 0;
}

输出:

1 -2.5 3 4 5.25 6 7 8 9.12
Number of floats: 9
1
-2.5
3
4
5.25
6
7
8
9.12