c++ indexing values from a .txt file and reorder them in a new file with a different structure -
i'm still c++ beginner, didn't myself find solution problems below.
i have 2 problems:
i want load , index values file
input.txt
structured follows:(x) (y) [colorname1, colorname2, colorname3, colorname4] [density1, density2, density3, density4]
- i create 10
vector
containers, don't know how "explain" program how find position of data structure of file...
- i create 10
example:
(notice same 'colorname'
, example: 'pink'
, can appear @ colorname3
or colorname2
, or anywhere in 4 colorname
positions)
this data structure of input.txt
file:
1 23 ['pinkwa', 'gb', 'pink', 'tuwa'][ 0.20078624 0.72376615 0.06735505 0.00809256] 1 24 ['pinkwa', 'gb', 'pink', 'tuwa'][ 0.19900636 0.72874652 0.06998165 0.00226546] 1 25 ['or0a', 'pink', 'pinkwa', 'gb'][ 0.00155317 0.07161603 0.19596207 0.73086873] 1 26 ['or0a', 'pink', 'pinkwa', 'gb'][ 0.00409427 0.07261927 0.19211352 0.73117294] 1 27 ['or0a', 'pink', 'pinkwa', 'gb'][ 0.00663538 0.07362251 0.18826497 0.73147715]
my second problem reordering imported data:
- i have list of
(x)
(y)
,colorname
i want access:
- for existing
(x)
(y)
duet and, - for given
colorname
colorname (x) (y) (density)
how can that? thank much
reading in data file pretty easy, if format little different:
instead of:
1 23 ['pinkwa', 'gb', 'pink', 'tuwa'][ 0.20078624 0.72376615 0.06735505 0.00809256]
write:
1 23 pinkwa gb pink tuwa 0.20078624 0.72376615 0.06735505 0.00809256
then use code:
class pointclass //i called pointclass because didn't know is, load there ;) { public: pointclass(){}; ~pointclass(){}; int x, y; std::string color1, color2, color3, color4; float density1, density2, density3, density4; }
loading:
ifstream in_stream; in_stream.open("input.txt"); std::vector<pointclass> points; while(!in_stream.eof()) { pointclass newpoint; in_stream >> newpoint.x >> newpoint.y; in_stream >> newpoint.color1 >> newpoint.color2 >> newpoint.color3 >> newpoint.color4; in_stream >> newpoint.density1 >> newpoint.density2 >> newpoint.density3 >> newpoint.density4; points.push_back(newpoint); }
then can whatever want data.
for accessing data specific x|y pair, thing comes mind searching through vector this:
bool searchpoint(std::vector<pointclass>* points, pointclass* out, int searchx, int searchy) { for(int i=0; i<points->size(); i++) { if(points->at(i).x != searchx) continue; if(points->at(i).y != searchy) continue; out = *(points->at(i)); return true; } return false; }
use function this:
// 'points' loaded vector pointclass* mypoint; bool result = searchpoint(&points, mypoint, 3, 5); // looking data @ x=3 | y=5 if(!result){ std::cout<<"no point data @ 3|5 found!"<<std::endl; }else{ // whatever want data: // example: std::cout<<"point data @ 3|5:"<<std::endl; std::cout<<mypoint->color1<<" "<<mypoint->color2<<" "<<mypoint->color3<<" "<<mypoint->color4<<std::endl; }
this code not tested should work , should give idea of need do... @ least hope so.
Comments
Post a Comment