вот дерево
Код |
#pragma once
#include <vector>
#include <string> #include <fstream>
namespace GUI {
class Node {public: std::vector<Node> childs; std::string Name; int pole1; int pole2; int level; public:
Node();
Node (const Node &obj) { childs = obj.childs; Name = obj.Name; }
void LoadData(std::string str) { Name=str; } int GetNumChilds () { return childs.size(); }
const Node & GetChild (int pos) const { return childs[pos]; }
Node & GetChild (int pos) { return childs[pos]; }
std::ostream& printFile(std::ostream& stream, unsigned int tab) const; std::istream& readFile (std::istream &stream); bool insertChild (int pos, Node & element ); bool removeChild (int pos); Node & operator = (const Node & from);
~Node();
}; }
|
Код |
#include "StdAfx.h" #include "MeTreeStruct.h"
GUI::Node::~Node() {
}
GUI::Node::Node() {
}
std::ostream & GUI::Node::printFile(std::ostream &stream, unsigned tab = 0) const { for(unsigned i = 0; i < tab; ++i) stream << ' ';
stream << "Name = ["; stream.write(Name.c_str(),Name.length()); stream << "]\n"; stream << "pole1 = [" << pole1 << "]/n"; stream << "pole2 = [" << pole2 << "]\n"; stream << "level = [" << level << "]\n"; for(std::vector<Node>::const_iterator i = childs.begin(), end = childs.end(); i != end; ++i) i->printFile(stream, tab + 1); return stream; }
bool GUI::Node::insertChild(int pos, Node & element) { std::vector<GUI::Node>::iterator p = GUI::Node::childs.begin(); p+=pos; GUI::Node::childs.insert(p,element); return true; }
GUI::Node & GUI::Node::operator =(const GUI::Node &from) { if (&from == this) return *this; childs = from.childs; Name = from.Name; return *this; }
bool GUI::Node::removeChild(int pos) { std::vector<GUI::Node>::iterator p = GUI::Node::childs.begin(); p+=pos; childs.erase(p); return GUI::Node::childs.empty(); }
std::istream & GUI::Node::readFile(std::istream &stream) { stream.ignore(8); stream >> Name; stream.ignore(9); stream >> pole1; stream.ignore(9); stream >> pole2; stream.ignore(9); stream >> level;
Node cld; std::vector<Node>::iterator i; for(int j = 0; j < level; ++j) { insertChild(j, cld); i = childs.begin()+j; i->readFile(stream); } return stream; }
|
при создании диалога, хочу прочитать файл, записать в дерево и вывести потомков первого уровня
Код | void CTreeApp::OnAppTree() { TreeDlg TrDlg; TrDlg.DoModal(); std::fstream inFile; inFile.open("Tree.txt",std::ios_base::in); if (!inFile) { MessageBox(0,TEXT("Err"),TEXT("Err"),MB_OK); return; }
root.readFile(inFile); TrDlg.root = root;
for(std::vector<GUI::Node>::iterator i = root.childs.begin(), end = root.childs.end(); i != end; ++i) {
TV_INSERTSTRUCT tvstruct; HTREEITEM pNode,pItm;
pItm = NULL; tvstruct.hParent = pItm; tvstruct.item.mask = TVIF_TEXT ;
tvstruct.item.pszText = LPWSTR(i->Name.c_str()); pNode = TrDlg.m_tree.InsertItem(&tvstruct); TrDlg.m_tree.SetFocus(); } }
|
Добавлено через 3 минуты и 32 секунды но не фурычит. не выводи, игнор. хотя ошибок нет. |