Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load_file编译错误 #26

Open
suiyuelichou opened this issue Oct 28, 2024 · 0 comments
Open

load_file编译错误 #26

suiyuelichou opened this issue Oct 28, 2024 · 0 comments

Comments

@suiyuelichou
Copy link

这部分代码是不是应该加一个类型判断,否则K,V的类型未知,编译器无法通过

// 从文件读取数据
template<class K, class V>
void SkipList<K, V>::load_file() {
    this->_file_reader.open("test.txt", ios::in);
    string line;

    while (getline(_file_reader, line)) {
        string key_str, value_str;
        get_key_value_from_string(line, &key_str, &value_str);

        if (key_str.empty() || value_str.empty()) {
            continue;
        }

        K key;
        V value;

        // 根据 K 的类型进行转换
        if constexpr (is_same<K, int>::value) {
            key = stoi(key_str);
        } else if constexpr (is_same<K, double>::value) {
            key = stod(key_str);
        } else {
            key = key_str; // 假设 K 是 string
        }

        // 根据 V 的类型进行转换
        if constexpr (is_same<V, int>::value) {
            value = stoi(value_str);
        } else if constexpr (is_same<V, double>::value) {
            value = stod(value_str);
        } else {
            value = value_str; // 假设 V 是 string
        }

        // 插入元素
        insert_element(key, value);
        cout << "key: " << key << " value: " << value << endl;
    }
    _file_reader.close();
}
@suiyuelichou suiyuelichou changed the title 文件加载编译错误 load_file编译错误 Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant