如何将特定向量转换为<string>字符串[n]

How can i convert a specific vector<string> to a string[n]

本文关键字:gt string 字符串 lt 向量 转换      更新时间:2023-10-16

这是字典文件中的向量:

//
//
//
//
#include <iostream>
#include <string>
#include <math.h>
#include <fstream>
#include <vector>
#include <regex>
using namespace std;
/**
 * Read file function
 * @params path : path of the file
 * @params skip_line : num of line to skip from begining
 * @params limit_line
 * @params parse_data : put data from file to array
 * @return : execution time
 */
long read_data(string path, int skip_line, int limit_line, vector<string> &parse_data) {
    long time = clock();
    ifstream stream(path);
    if (!stream) {
        cerr << "File : " + path + " not found." << endl;
        return -1;
    }
    string line; int count = 0;
    while (getline(stream, line)) {
        if (count<skip_line) {
            count++;
            continue;
        }
        if (count >= limit_line && limit_line > 0) {
            break;
        }
        if (line.empty()) {
            continue;
        }
        count++;
        try{
            string delimiter_keyword = "#";
            string delimiter_translate = "*";
            string pre_translate = line.substr(0, line.find(delimiter_translate));
            string translate = line.substr(line.find(delimiter_translate)+1);
            string key_word = pre_translate.substr(0, pre_translate.find(delimiter_keyword));
            string pronounce_word = pre_translate.substr(pre_translate.find(delimiter_keyword));
//            cout << translate << endl;
//            cout << pronounce_word << endl;
            parse_data.push_back(key_word);
        } catch(exception ex) {
            // parse exception
        }
    }
    stream.close();
    time = (clock() - time)/ double(CLOCKS_PER_SEC) * 1000;
    return time;
}
long heap_sort(){
    long time = clock();
    /**
     * Help code here.
     */
    time = (clock() - time)/ double(CLOCKS_PER_SEC) * 1000;
    return time;
}
long quick_sort(){
    long time = clock();
    /**
     * Help code here.
     */
    time = (clock() - time)/ double(CLOCKS_PER_SEC) * 1000;
    return time;
}

int main() {
    int limit_line = 0;
    vector<string> dictionary_data;
    int exec_time = read_data(
            "D:Nam 2Cau truc du lieu va giai thuatav.dd",
            3, limit_line, dictionary_data
    );
    cout << "Total time : " << exec_time << " ms" << endl;

    for (string line : dictionary_data) {
        cout << line << endl;
    }
}

我如何将这个向量转换为字符串[n],以便将我的排序算法放入其中?感谢

如何将此矢量转换为字符串[n]

向量不能转换为数组

您可以做的是将向量的内容复制到数组中。但你不想这样做,因为在编译时你通常不知道向量会有多大

这样我就可以把我的排序算法放进去了?

您不需要将向量"转换"为数组。按原样使用矢量中包含的数组。