如何将特定向量转换为<string>字符串[n]
How can i convert a specific vector<string> to a string[n]
这是字典文件中的向量:
//
//
//
//
#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]
向量不能转换为数组。
您可以做的是将向量的内容复制到数组中。但你不想这样做,因为在编译时你通常不知道向量会有多大
这样我就可以把我的排序算法放进去了?
您不需要将向量"转换"为数组。按原样使用矢量中包含的数组。
相关文章:
- cppcheck在const std::string[]上引发警告
- 将std::string传递给WriteConsole API
- 为std::string的某个索引赋值
- std中有类似find_last_of的函数,而string中没有
- EASTL矢量<向量<int>>连续的
- 使用 std::string () const 函数启动线程或未来
- 使用char类型将decimal转换为string,将string转换为decimal
- 迭代和比较映射<字符串、矢量<string>> c++ 中的值
- 当我们进行一些操作时,应该使用什么'std::string'或'std::stringstream'?
- 将向量解析<string>为字符串
- 'string.assign(string.data(), 5)' 是明确定义的还是 UB?
- 如何更改大小(std::string)
- "string.h"在构建适用于iOS的qt应用程序中找不到消息
- C++:如何将 unix 时间的字符串转换为 *tm?(使用时间错误:"cannot convert 'String' to 'tm*' ")
- std::string 的对象真的可以移动吗?
- 与'operator='不匹配(操作数类型'String'且"void")
- SegFault 同时使用 std::string::operator+= 和函数作为参数
- 无法从 std::string 中提取C++ Unicode 符号
- std::string 构造函数如何处理固定大小的 char[]?
- <string> 使用 for 循环写入向量