我需要创建一个函数来获取字符串中的这些数字
I need to create a function that gets those numbers in the string
我正在尝试使一个接受参数的函数应该是这样的
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
相关文章:
- 从C字符串中获取奇怪的字符串长度
- 获取字符串的长度并将其分配给数组
- 我如何获取从 C++ 到 C 的映射<字符串、int>#
- 调用CreateProcess()并获取字符串的返回值
- 从字符串中获取结构的大小
- Cryptopp:获取密码输入的填充字符串
- 获取无效的字符串大小
- 获取 OID(类型::b_oid)作为 MongoDB C++驱动程序中的字符串
- C ++编译器如何获取argv字符串的长度
- 给枚举一个整数,并在 C++ 中将其相关值作为字符串获取
- 如何使用字符串获取用户控制台输入
- 通过字符串获取小部件
- 无法从字符串获取数字输入
- 在C++的静态方法中将类名作为字符串获取
- 从设备实例路径字符串获取设备实例 DWORD
- HRESULT到字符串(获取文档路径)C++
- 如何从http请求体字符串获取文件名
- 如何使用带有垃圾字符的子字符串获取字符串
- 通过字符串获取对话框项的可能性
- 有没有办法将 WinAPI 错误标志作为字符串获取