Click here to Skip to main content
15,115,909 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: How to constructor a storage class to store Tree data in C++ Pin
Richard MacCutchan25-Oct-14 23:09
mveRichard MacCutchan25-Oct-14 23:09 
GeneralRe: How to constructor a storage class to store Tree data in C++ Pin
Orjan Westin27-Oct-14 4:54
professionalOrjan Westin27-Oct-14 4:54 
GeneralRe: How to constructor a storage class to store Tree data in C++ Pin
Richard MacCutchan27-Oct-14 8:41
mveRichard MacCutchan27-Oct-14 8:41 
GeneralRe: How to constructor a storage class to store Tree data in C++ Pin
Orjan Westin28-Oct-14 0:13
professionalOrjan Westin28-Oct-14 0:13 
GeneralRe: How to constructor a storage class to store Tree data in C++ Pin
Richard MacCutchan28-Oct-14 0:56
mveRichard MacCutchan28-Oct-14 0:56 
AnswerRe: How to constructor a storage class to store Tree data in C++ Pin
Orjan Westin27-Oct-14 4:52
professionalOrjan Westin27-Oct-14 4:52 
AnswerRe: How to constructor a storage class to store Tree data in C++ Pin
Stefan_Lang27-Oct-14 5:25
mveStefan_Lang27-Oct-14 5:25 
AnswerRe: How to constructor a storage class to store Tree data in C++ Pin
Vaclav Naydenov28-Oct-14 12:42
MemberVaclav Naydenov28-Oct-14 12:42 
You can use a struct or a class with nested vector of same type structs:

C++
#include <vector>
#include <string>
#include <iostream>

struct TreeNode {
    std::string payload;
    std::vector<TreeNode> children;
    TreeNode(const std::string &p): payload(p) {}
    TreeNode() {} // it's a must if we are to store nodes in a vector
};

void walk_tree(TreeNode &node, int indent = 0) {
    std::cout << std::string(indent * 4, ' ')
        << node.payload << std::endl;
    for (size_t i = 0; i < node.children.size(); ++i)
        walk_tree(node.children[i], indent + 1);
}

int main() {
    TreeNode a("home");
    a.children.push_back(TreeNode("peter"));
    a.children.push_back(TreeNode("jane"));
    a.children[0].children.push_back(TreeNode("game.exe"));
    a.children[1].children.push_back(TreeNode("homework.txt"));
    walk_tree(a);
    return 0;
}

QuestionCannot initialize array of classes except as a local variable. Pin
Vaclav_24-Oct-14 9:19
MemberVaclav_24-Oct-14 9:19 
AnswerRe: Cannot initialize array of classes except as a local variable. Pin
Chris Losinger24-Oct-14 10:09
professionalChris Losinger24-Oct-14 10:09 
GeneralRe: Cannot initialize array of classes except as a local variable. Pin
Vaclav_24-Oct-14 10:19
MemberVaclav_24-Oct-14 10:19 
QuestionRe: Cannot initialize array of classes except as a local variable. Pin
David Crow24-Oct-14 11:50
MemberDavid Crow24-Oct-14 11:50 
AnswerRe: Cannot initialize array of classes except as a local variable. Pin
Vaclav_24-Oct-14 16:04
MemberVaclav_24-Oct-14 16:04 
AnswerRe: Cannot initialize array of classes except as a local variable. Pin
Stefan_Lang27-Oct-14 5:02
mveStefan_Lang27-Oct-14 5:02 
QuestionRecursive function - how to correctly implement return? Pin
Vaclav_22-Oct-14 10:20
MemberVaclav_22-Oct-14 10:20 
AnswerRe: Recursive function - how to correctly implement return? Pin
Richard Andrew x6422-Oct-14 11:06
professionalRichard Andrew x6422-Oct-14 11:06 
SuggestionRe: Recursive function - how to correctly implement return? Pin
«_Superman_»22-Oct-14 19:56
professional«_Superman_»22-Oct-14 19:56 
GeneralRe: Recursive function - how to correctly implement return? Pin
Vaclav_23-Oct-14 6:57
MemberVaclav_23-Oct-14 6:57 
AnswerRe: Recursive function - how to correctly implement return? Pin
Stefan_Lang23-Oct-14 2:57
mveStefan_Lang23-Oct-14 2:57 
GeneralRe: Recursive function - how to correctly implement return? Pin
Vaclav_23-Oct-14 6:53
MemberVaclav_23-Oct-14 6:53 
SuggestionRe: Recursive function - how to correctly implement return? Pin
David Crow23-Oct-14 10:23
MemberDavid Crow23-Oct-14 10:23 
AnswerRe: Recursive function - how to correctly implement return? Pin
CPallini24-Oct-14 0:35
mveCPallini24-Oct-14 0:35 
QuestionOccluded Objects Pin
HAMUDA7822-Oct-14 3:31
MemberHAMUDA7822-Oct-14 3:31 
QuestionRe: Occluded Objects Pin
David Crow22-Oct-14 7:17
MemberDavid Crow22-Oct-14 7:17 
SuggestionRe: Occluded Objects Pin
Stefan_Lang23-Oct-14 3:08
mveStefan_Lang23-Oct-14 3:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.