我想创建一个模板类的对象" stack"使用如下所示的if条件并在if条件之外使用它。
template <class TYPE>
class stack{
..
..
};
main()
{
int opt;
cin>>opt;
这是if条件
if(opt == 1)
stack<int> s;
else
stack<float> s;
s.push(1);
}
我面临的问题是对象&#34; s&#34;正在块范围内创建,如果条件,我无法访问该对象。
答案 0 :(得分:1)
这很简单。只需将公共代码放在函数模板中即可。
template< class Number >
void foo()
{
stack<Number> s;
s.push( 1 );
}
auto main() -> int
{
int opt;
cin>>opt;
opt == 1? foo<int>() : foo<double>();
}
答案 1 :(得分:1)
require_once "...filename with get_session_user_id() function...";
的类型必须在运行时之前知道,但您可以使用这样的接口:
s
答案 2 :(得分:0)
最后,我找到了答案。
以&#34;纯虚拟&#34;制作功能。在基类。
在派生类中定义纯虚函数。
在main()中声明基类型指针* s,
然后在if条件下package patriciaTrie;
import java.util.ArrayList;
import java.util.Scanner;
public class Patricia {
private TrieNode nodeRoot;
private TrieNode nodeFirst;
// create a new node
public Patricia() {
nodeRoot = null;
}
// inserts a string into the trie
public void insert(String s) {
if (nodeRoot == null) {
nodeRoot = new TrieNode();
nodeFirst = new TrieNode(s);
nodeFirst.isWord = true;
nodeRoot.next.add(nodeFirst);
} else {
// nodeRoot.isWrod = false;
insert(nodeRoot, s);
}
}
private String checkEdgeString(ArrayList<TrieNode> history, String s) {
StringBuilder sb = new StringBuilder();
for (TrieNode nextNodeEdge : history) {
int len1 = nextNodeEdge.edge.length();
int len2 = s.length();
int len = Math.min(len1, len2);
for (int index = 0; index < len; index++) {
if (s.charAt(index) != nextNodeEdge.edge.charAt(index)) {
break;
} else {
char c = s.charAt(index);
sb.append(c);
}
}
}
return sb.toString();
}
private void insert(TrieNode node, String s) {
ArrayList<TrieNode> history = new ArrayList<TrieNode>();
for (TrieNode nextNodeEdge : node.getNext()) {
history.add(nextNodeEdge);
}
String communsubString = checkEdgeString(history, s);
System.out.println("commun String: " + communsubString);
if (!communsubString.isEmpty()) {
for (TrieNode nextNode : node.getNext()) {
if (nextNode.edge.startsWith(communsubString)) {
String substringSplit1 = nextNode.edge
.substring(communsubString.length());
String substringSplit2 = s.substring(communsubString
.length());
if (substringSplit1.isEmpty() && !substringSplit2.isEmpty()) {
// 1. case: aba, abaxyz
} else if (substringSplit2.isEmpty()
&& !substringSplit1.isEmpty()) {
// 2. case: abaxyz, aba
ArrayList<TrieNode> cacheNextNode = new ArrayList<TrieNode>();
System.out.println("node edge string is longer.");
if (nextNode.getNext() != null && !nextNode.getNext().isEmpty()) {
for (TrieNode subword : nextNode.getNext()) {
subword.edge = substringSplit1.concat(subword.edge); //This line
cacheNextNode.add(subword);
}
nextNode.getNext().clear();
nextNode.edge = communsubString;
nextNode.isWord = true;
TrieNode child = new TrieNode(substringSplit1);
child.isWord = true;
nextNode.next.add(child);
for(TrieNode node1 : cacheNextNode){
child.next.add(node1);
System.out.println("Test one");
}
cacheNextNode.clear();
}else{
nextNode.edge = communsubString;
TrieNode child = new TrieNode(substringSplit1);
child.isWord = true;
nextNode.next.add(child);
System.out.println("TEST");
}
} else if(substringSplit1.isEmpty() && substringSplit2.isEmpty()){
//3. case: aba and aba.
nextNode.isWord = true;
}else {
// 4. Case: abauwt and abaxyz
//if(nextNode.getNext().isEmpty())
}
break;
}
}
} else {
// There is no commun substring.
System.out.println("There is no commun substring");
TrieNode child = new TrieNode(s);
child.isWord = true;
node.next.add(child);
}
}
public static void main(String[] args) {
Patricia p = new Patricia();
Scanner s = new Scanner(System.in);
while (s.hasNext()) {
String op = s.next();
if (op.equals("INSERT")) {
p.insert(s.next());
}
}
}
class TrieNode {
ArrayList<TrieNode> next = new ArrayList<TrieNode>();
String edge;
boolean isWord;
// To create normal node.
TrieNode(String edge) {
this.edge = edge;
}
// To create the root node.
TrieNode() {
this.edge = "";
}
public ArrayList<TrieNode> getNext() {
return next;
}
public String getEdge() {
return edge;
}
}
}
或
s = new stackDerived<int>
。
s = new stackDerived<char>