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

时间:2016-04-13 15:51:33

标签: c++ string vector

这是我的字典文件中的向量:

//
//
//
//
#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 2\Cau truc du lieu va giai thuat\av.dd",
            3, limit_line, dictionary_data
    );
    cout << "Total time : " << exec_time << " ms" << endl;


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

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

1 个答案:

答案 0 :(得分:0)

  

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

矢量无法转换到数组。

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

  

所以我可以把我的algothrims放进去吗?

为了做到这一点,您不需要将矢量“转换”为数组。按原样使用向量中包含的数组。