I am using an iterator in a C++ code to retrieve records read using sqlite3 statements. I am able to display the contents pointed to by the iterator to screen using cout
. How would i assign the value to a simple float
or array variable.
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.
For float for example you could use 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
:
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:
float x = stof(*iter_jj);
(Obviously x
could be a variable outside of the loop).
If you don't have 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.
The good approach would be constructing and using std::istringstream
object for this purpose:
float f;
std::istringstream is(*iter_jj);
if (is >> f)
{
// handle successful retrieval...
}
just don't forget to #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*/);
in c++98, you can write your own 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
. 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 )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.