简体   繁体   English

C ++迭代器值转换为变量

[英]C++ iterator value to variable

I am using an iterator in a C++ code to retrieve records read using sqlite3 statements. 我在C ++代码中使用迭代器来检索使用sqlite3语句读取的记录。 I am able to display the contents pointed to by the iterator to screen using cout . 我可以使用cout在屏幕上显示迭代器指向的内容。 How would i assign the value to a simple float or array variable. 我如何将值分配给简单的float或array变量。

typedef vector<vector<string> > Records;
vector< vector<string> >::iterator iter_ii;
vector<string>::iterator iter_jj;

Records records = select_stmt("SELECT density FROM Ftable where PROG=2.0");

  for(iter_ii=records.begin(); iter_ii!=records.end(); iter_ii++)
   {
      for(iter_jj=(*iter_ii).begin(); iter_jj!=(*iter_ii).end(); iter_jj++)
      {
         cout << *iter_jj << endl; //This works fine and data gets displayed!

         //How do i store the data pointed to by *iter_jj in a simple float variable or array?
      }
   }

C++ is type-safe, so you need to explicitly convert the string to the desired target type. C ++是类型安全的,因此您需要将字符串显式转换为所需的目标类型。

For float for example you could use atof : 例如对于float,您可以使用atof

float f = atof(iter_jj->c_str());

A more convenient alternative is Boost's lexical_cast , which works with the same syntax for all types that support extraction from an std::istream : 一个更方便的替代方法是Boost的lexical_cast ,它对支持从std::istream提取的所有类型使用相同的语法:

float f = boost::lexical_cast<float>(*iter_jj);

Note that both of these can fail in different ways if the contents of the string cannot be converted to a float in any meaningful way. 请注意,如果不能以任何有意义的方式将字符串的内容转换为浮点数,则这两种方式都可能以不同的方式失败。

Your real problem is how to convert a string to a float. 您真正的问题是如何将字符串转换为浮点数。 Here is one solution. 这是一个解决方案。

float value;
stringstream ss(*iter_jj);
if (! (ss >> value))
{
    ERROR failed to convert value
}

If you have C++11 compatible compiler: 如果您具有C ++ 11兼容的编译器:

float x = stof(*iter_jj);

(Obviously x could be a variable outside of the loop). (显然, x可能是循环外的变量)。

If you don't have C++11: 如果您没有C ++ 11:

stringstream ss(*iter_jj);
float x;
ss >> x;

Well since you are working with: 好吧,因为您正在使用:

std::vector<std::vector<std::string> > records;

the actual question here is: how to retrieve the specific type of data from std::string object. 实际的问题是:如何从std::string对象检索特定类型的数据。

The good approach would be constructing and using std::istringstream object for this purpose: 好的方法是为此目的构造和使用std::istringstream对象:

float f;
std::istringstream is(*iter_jj);
if (is >> f)
{
    // handle successful retrieval...
}

just don't forget to #include <sstream> :) 只是不要忘记#include <sstream> :)

As for converting * to string, in c++11, you can convert integer/floats to string by calling static method to_string: string str = std::string::to_string(integer/* or float*/); 至于将*转换为字符串,在c ++ 11中,可以通过调用静态方法to_string将整数/浮点数转换为字符串: string str = std::string::to_string(integer/* or float*/);

in c++98, you can write your own to_string: 在c ++ 98中,您可以编写自己的to_string:

#include <cstdio>
#include <cstring>
#include <cstdarg>
#include <string>
void format_aux(char* ptr, int size, const char* format, ...) {
    va_list args;
    va_start(args, format);
    vsnprintf(ptr, size, format, args);
    va_end(args);
}

#undef TO_STRING__GEN
#define TO_STRING__GEN(type, arg, size)     \
std::string                                 \
to_string(type val) {                       \
    const int sz = size;                    \
    char buf[sz];                           \
    format_aux(buf, sz, arg, val);          \
    return std::string(buf);                \
}                                           

TO_STRING__GEN(int,           "%d", 4*sizeof(int))
TO_STRING__GEN(unsigned int,  "%u", 4*sizeof(unsigned int))
TO_STRING__GEN(long,          "%ld", 4*sizeof(long))
TO_STRING__GEN(unsigned long, "%lu", 4*sizeof(unsigned long))
TO_STRING__GEN(float,         "%f", (std::numeric_limits<float>::max_exponent10 + 20))
TO_STRING__GEN(double,        "%f", (std::numeric_limits<float>::max_exponent10 + 20))

#undef TO_STRING__GEN

*iter_jj is going to give you a std::string . *iter_jj将为您提供std::string In order to store that as a float , it will need to be a floating point number in string form (eg "1.23456" ) and you will need to call one of the strtof family of functions ( http://en.cppreference.com/w/cpp/string/byte/strtof ) 为了将其存储为float ,它必须是字符串形式的浮点数(例如"1.23456" ),并且您需要调用strtof函数家族之一( http://en.cppreference.com / w / cpp / string / byte / strtof

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM