如何将字符串转换为数字

how can I get string to number?

本文关键字:数字 转换 字符串      更新时间:2023-10-16

我正在研究数据收集,并试图扫描标题并将其转换为实数,使用"sscanf",但它没有像我期望的那样给我数字,编写的"拆分函数"正在帮助我拆分字符串向量:

#include <iostream>   // std::cout
#include <string>     // std::string, std::stod

int main() {
   string fn = "ch-683-mhz-8000.0-ksps-2016-06-20-17.24.19-utc.dat";
   vector<string> v;
   int f0, fs;
   split(fn, '-', v);
   for (int i = 0; i < v.size(); ++i) {
      cout << i << "   " << v[i] << 'n';
   }
   for(unsigned i=1; i < v.size(); i++){
       if(v[i] == "mhz"){
          std::sscanf(v[i-1], &f0);
          int ret = sscanf(v[i-1], &f0);
          cout << v[i-1] ;
       }
   }
   return 0;
}

的"向量v"将给我的结果:683兆赫8000.0过度增殖20160620.17.24.19utc.dat

和我想把683和8000转换成实数,但我收到了错误,即使我试图从论坛做一些搜索:

Description Resource    Path    Location    Type
'resize' is ambiguous '
Candidates are:
void resize(unsigned long int, std::complex<float>)
'   fft.hpp /test2/src  line 186    Semantic Error
recipe for target 'src/test2.o' failed  subdir.mk   /test2/Debug/src    line 18 C/C++ Problem
'resize' is ambiguous '
Candidates are:
void resize(unsigned long int, std::complex<float>)
'   fft.hpp /test2/src  line 251    Semantic Error
cannot convert ‘std::__cxx11::basic_string<char>’ to ‘const char*’ for argument ‘1’ to ‘int sscanf(const char*, const char*, ...)’  test2.cpp   /test2/src  line 495    C/C++ Problem
cannot convert ‘std::__cxx11::basic_string<char>’ to ‘const char*’ for argument ‘1’ to ‘int sscanf(const char*, const char*, ...)’  test2.cpp   /test2/src  line 494    C/C++ Problem
Invalid arguments '
Candidates are:
int sscanf(const char *, const char *, ...)
'   test2.cpp   /test2/src  line 494    Semantic Error
Invalid arguments '
Candidates are:
int sscanf(const char *, const char *, ...)
'   test2.cpp   /test2/src  line 495    Semantic Error
make: *** [src/test2.o] Error 1 test2           C/C++ Problem

有人能帮忙吗?

int main()
{
    string fn = "ch-683-mhz-8000.0-ksps-2016-06-20-17.24.19-utc.dat";
    string szt1;
    for (int i = 0; i < 15; i++)
    {
        szt1 = szt1 + fn[i];
    }

    int n1;
    int n2;
    string szn1;
    string szn2;
    for (int i = 3; i < szt1.length(); i++)
    {
        if (i < 6)
        {
            szn1 = szn1 + szt1[i];
        }
    }
    for (int i = 11; i < szt1.length(); i++)
    {
            szn2 = szn2 + szt1[i];
    }
    n1 = atoi(szn1.c_str());
    n2 = atoi(szn2.c_str());
    cout << n1 << endl;
    cout << n2 << endl;
    return 0;
}