Iostream iterator. String to float and long

Source: Internet
Author: User

// Iostream iterator usage

/* Requirement: Read the data RX ry rz tx ty TZ frame from input.txt. The data format is as follows:

Tools Port 2: SJTU 100 s/n: Drawing 14c00 frame face state RX ry rz tx ty TZ error markers frame state TX ty TZ
1 2 159366 1 OK-42.6259 10.6095-19.0342-53.35-282.09 2124.58 0 0.1529
1 2 159367 1 OK-42.5965 10.643-19.1422-53.52-281.64 2124.7 0 0.1754
1 2 159368 1 OK-42.6655 10.6502-19.222-53.65-281.35 2124.79 0 0.1813
1 2 159369 1 OK-42.7368 10.6059-19.2744-53.75-281.22 2124.84 0 0.168

Solution:
Use the <string> type istream_iterator
Istream_iterator <string> ins (infile), Eos;
Use the find () algorithm to locate string "OK" and then use the auto-increment operator to locate the desired data location and read the corresponding data.

The read string is converted to a C-type character array by using the c_str () method, and then converted to the double type by using atof (float type only). atol is converted to the long type, and then stored
*/
 
// ******************* The data Class header file vec6.h is as follows *************** ***********
 
# Ifndef vec6_h
# Define vec6_h

# Include <vector>
# Include <algorithm>
# Include <iterator>
# Include <iostream>
# Include <iomanip>
# Include <fstream>
# Include <string>
Using namespace STD;

Class vec6
{
Public:
Vec6 (const long & frame, const float & RX, const float & ry, const float & RZ, const float & Tx, const float & ty, const float & Tz );
Static void show (vector <vec6> & vec6list );
Static vector <vec6> readfromfile (const string & strfilename); // read data from the input file to construct a vector of the vec6 type and return a copy.
PRIVATE:
Long m_frame;
Float m_rx;
Float m_ry;
Float m_rz;
Float m_tx;
Float m_ty;
Float m_tz;
};

// Constructor
Vec6: vec6 (const long & frame, const float & RX, const float & ry, const float & RZ, const float & Tx, const float & ty, const float & Tz)
{
M_frame = frame;
M_rx = RX;
M_ry = ry;
M_rz = RZ;
M_tx = TX;
M_ty = ty;
M_tz = TZ;
}

// Build vec6 vector from data file
Vector <vec6> vec6: readfromfile (const STD: string & strfilename)
{
Vector <vec6> vec6_result;
Float * fdatabuf = new float [6];
Long lframebuf = 0;
Ifstream inputfile;
Inputfile. Open (strfilename. c_str (), IOS: In | IOs: _ nocreate); // you must use the _ nocreate mode to determine whether the file to be opened exists.
If (inputfile. Fail ())
{
Cout <"cannot read input file! ";
Throw "cannot read input file! ";
};

Istream_iterator <string> is (inputfile), Eos; // The EOS created by the default constructor is equivalent to invalid iterator. EOS (default iterator) is invalid iterator
While (! Inputfile. EOF ())
{
Is = find (is, EOS, "OK"); // find "OK" from the beginning of istream ". Narrow down the scope.
If (! Is. _ equal (EOS) // compare iterator using the _ equals () method to ensure that find finds "OK" and returns a valid value, otherwise, it indicates that the last row has exceeded and the operation should be completed.
{
Is ++;
// Begin Input
For (INT I = 0; I <6; I ++, ++ is)
{
// Cout <(* Is). c_str () <Endl;
Fdatabuf [I] = atof (* Is). c_str (); // string to float type
};
For (INT I = 0; I <2; I ++)
{
++ Is;
};
Lframebuf = atol (* Is). c_str (); // convert string to long type
Vec6 * pvec6 = new vec6 (lframebuf, fdatabuf [0], fdatabuf [1], fdatabuf [2], fdatabuf [3], fdatabuf [4], fdatabuf [5]);
Vec6_result.push_back (* pvec6);/* vector. push_back (T & X) transmits parameters through reference on the surface. In fact, the referenced variables are copied internally rather than simply referenced, otherwise, the survival time of the variable pointed to by pvec6 is only a function field, which leads to incorrect elements in the vector. The internal implementation of push_back (T & X) is to call the placement new operator new (finish) T (& X) the T-type variables (pp122 and pp51 in STL source code analysis) are constructed with the & X as the parameter at the position indicated by the finish pointer of the vector container )*/
// Cout <"end of a row" <Endl;
}
}

Inputfile. Close (); // do not forget to close the input file.
Return vec6_result;
}

// Iterate and show vector <vec6>
Void vec6: Show (vector <vec6> & vec6list)
{
Cout <"begin output: "<Endl <'\ t' <" <frame> "<' \ t' <" <RX> "<'\ t' <" <ry> "<'\ t' <" <RZ> "<' \ t' <" <TX> "<'\ t' <" <ty> "<'\ t' <" <TZ> "<Endl;
For (vector <vec6 >:: iterator it = vec6list. Begin (); it! = Vec6list. End (); It ++)
{
Cout <"vec6: "<it-> m_frame <'\ t' <it-> m_rx <' \ t' <it-> m_ry <'\ t' <it -> m_rz <'\ t' <it-> m_tx <' \ t' <it-> m_ty <'\ t' <it-> m_tz <<Endl;
};
}

//
# Endif

// ************************** EOF vec6.h *********** ********

// ********************* Main file vec6.cpp *************** ***

// Vec6.cpp: defines the entry point of the console application.
//

# Include "stdafx. H"
# Include "vec6.h"
Using namespace STD;

Int _ tmain (INT argc, _ tchar * argv [])
{
Vector <vec6> dataset;
Try
{
Dataset = vec6: readfromfile (". \ in.txt ");
}
Catch (...)
{
Cout <"error reading input file." <Endl;
}
If (! Dataset. Empty ())
{
Vec6: Show (Dataset );
}
Else cout <"dataset is empty." <Endl;

System ("pause ");
Return 0;
}

// ********************* EOF vec6.cpp *************** *****

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.