Last active
August 16, 2019 03:04
-
-
Save Benjamin1021523/41488a33d85f031a88f79f196814a87d to your computer and use it in GitHub Desktop.
c++的多維陣列嘗試
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//c++想玩出不定維度陣列也是辦得到的,但是元素資料型態不固定大概就不行了。 | |
#include<iostream> | |
using namespace std; | |
template <typename T> | |
struct Node{ | |
T val; | |
Node* next; | |
Node* child; | |
Node(T n){ | |
val = n; | |
next = NULL; | |
child = NULL; | |
} | |
}; | |
void Integer(){ | |
Node<int> head(1); | |
Node<int>* p = &head; | |
for(int i = 2;i < 6;i++){ | |
p->next = new Node<int>(i); | |
p->child = new Node<int>(0); | |
p = p->next; | |
} | |
p->child = new Node<int>(0); | |
for(Node<int>* i = &head;i;i = i->next){ | |
cout << " " << i->val; | |
for(Node<int>* j = i;j->child;j = j->child) | |
cout << " -- " << j->child->val; | |
cout << endl << " | " << endl; | |
} | |
} | |
void Double(){ | |
Node<double> head(1.1); | |
Node<double>* p = &head; | |
for(double i = 2.2;i < 7;i += 1.1){ | |
p->next = new Node<double>(i); | |
p->child = new Node<double>(0); | |
p = p->next; | |
} | |
p->child = new Node<double>(0); | |
for(Node<double>* i = &head;i;i = i->next){ | |
cout << " " << i->val; | |
for(Node<double>* j = i;j->child;j = j->child) | |
cout << " -- " << i->child->val; | |
cout << endl << " | " << endl; | |
} | |
} | |
int main(){ | |
Integer(); | |
cout << endl; | |
Double(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//繼續合併還可以變成這樣呢 | |
#include<iostream> | |
using namespace std; | |
template <typename T> | |
struct Node{ | |
T val; | |
Node* next; | |
Node* child; | |
Node(T n){ | |
val = n; | |
next = NULL; | |
child = NULL; | |
} | |
Node(){ | |
next = NULL; | |
child = NULL; | |
} | |
}; | |
template <typename T> | |
void func(T val){ | |
Node<T> head(val); | |
Node<T>* p = &head; | |
T temp = val; | |
for(int i = 2;i < 6;i++, temp += val){ | |
p->next = new Node<T>(temp + val); | |
p->child = new Node<T>(temp); | |
p = p->next; | |
} | |
p->child = new Node<T>(); | |
for(Node<T>* i = &head;i;i = i->next){ | |
cout << " " << i->val; | |
for(Node<T>* j = i;j->child;j = j->child) | |
cout << " -- " << j->child->val; | |
if(i->next) | |
cout << endl << " | "; | |
cout << endl; | |
} | |
} | |
int main(){ | |
func(1); | |
cout << endl; | |
func(1.2); | |
cout << endl; | |
func(string("x")); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment