qstring 转string_view
时间: 2023-08-04 15:04:12 浏览: 37
要将 QString 转换为 std::string_view,您可以使用 QString 的 toStdString() 函数来获取 std::string 对象,然后使用 std::string_view 构造函数将其转换为 std::string_view。下面是一个示例代码:
```cpp
#include <iostream>
#include <string>
#include <string_view>
#include <QString>
int main() {
QString qstr = "Hello, World!";
std::string str = qstr.toStdString();
std::string_view strView(str);
std::cout << strView << std::endl;
return 0;
}
```
这个示例中,首先使用 toStdString() 将 QString 转换为 std::string,然后使用 std::string 的构造函数将其转换为 std::string_view。最后,可以使用 std::cout 输出 std::string_view。
请注意,由于 std::string_view 是一个视图类型,它不拥有字符串的内存。因此,在使用 std::string_view 时要确保源字符串的生命周期足够长。
相关问题
优化这段代码 for(auto begin=str.cbegin(); begin+1!=str.cend();begin++){ if (*begin == '0' && *(begin+1) == 'x'){ //0x分断,取后面 QStringList str_list = str.split("0x"); //:分断,取前面 QStringList str_list2 = str_list[1].split(":"); quint16 a = str_list2.at(0).toInt(nullptr,16); return a; } } return 0;
可以尝试对代码进行以下优化:
1. 使用迭代器代替 cbegin() 和 cend(),避免每次循环都要计算一遍。
2. 使用 std::string_view 替换 std::string,减少内存拷贝。
3. 避免多次分割字符串,可以使用正则表达式提取所需的部分。
4. 将十六进制字符串转换成整数可以使用 std::stoi 函数,避免手动转换。
优化后的代码如下:
```c++
#include <regex>
#include <string_view>
quint16 hexStringToUInt16(const std::string_view str)
{
static const std::regex pattern("0x([0-9a-fA-F]+):");
std::smatch match;
if (std::regex_search(str.begin(), str.end(), match, pattern))
{
return std::stoi(match[1].str(), nullptr, 16);
}
return 0;
}
```
这里使用了正则表达式提取字符串中的十六进制数字部分,然后使用 std::stoi 函数将其转换成整数。同时避免了多次分割字符串,提高了效率。
QTableView和tablewidget转换
密函数
string decrypt(string ciphertext, string key) {
// 进行IP置换
string permuted = applyIP(ciphertextQTableView和QTableWidget是Qt中两个常用的表格控件,二者的最大区别在于Q);
// 分离左右两半
string left = permuted.substr(0, 4);
string right = permutedTableView是一个基于Model/View架构的表格视图,而QTableWidget则是一个单独的表格控.substr(4, 4);
// 获取子密钥
string subkey1 = getSubkey(key, 1);
件。因此,直接将QTableView转换为QTableWidget是不可行的。
如果您需要将一个QTableView string subkey2 = getSubkey(key, 0);
// Round 1
string expanded = right[3] + right的内容显示在一个QTableWidget中,可以通过以下步骤实现:
1. 创建一个QTableWidget,并设置表格 + right[0];
string xorResult = "";
for (int i = 0; i < expanded.length(); i++) {
if (expanded[i] == subkey1[i]) {
xorResult += "0";
} else {
xorResult += "1的行数、列数和表头标签等属性;
2. 创建一个QStandardItemModel,并使用setRowCount()和setColumn";
}
}
int sBoxResult1 = applySBox(S_BOX1, xorResult.substr(0, 4));
Count()函数设置模型的行数和列数;
3. 将QTableView中的数据复制到QStandardItemModel中;
int sBoxResult2 = applySBox(S_BOX2, xorResult.substr(4, 4));
string sBoxOutput = decimal4. 将QStandardItemModel设置为QTableWidget的模型。
下面是示例代码:
```
// 创建QTableWidgetToBinary(sBoxResult1, 2) + decimalToBinary(sBoxResult2, 2);
string p4Output = apply
QTableWidget *tableWidget = new QTableWidget(this);
tableWidget->setRowCount(tableView->model()->rowCount());
tableWidget->setP4(sBoxOutput);
xorResult = "";
for (int i = 0; i < p4Output.length(); i++)ColumnCount(tableView->model()->columnCount());
tableWidget->setHorizontalHeaderLabels(QStringList() << "Column 1" << "Column {
if (p4Output[i] == left[i]) {
xorResult += "0";
} else {
xorResult +=2" << "Column 3");
// 创建QStandardItemModel
QStandardItemModel *standardModel = new QStandardItemModel(this);
standardModel "1";
}
}
left = right;
right = xorResult;
// Round 2
expanded = right[->setRowCount(tableView->model()->rowCount());
standardModel->setColumnCount(tableView->model()->columnCount());
// 复制数据到QStandardItem3] + right + right[0];
xorResult = "";
for (int i = 0; i < expanded.length(); iModel中
for (int row = 0; row < tableView->model()->rowCount(); ++row) {
for (int col =++) {
if (expanded[i] == subkey2[i]) {
xorResult += "0";
} else {
xorResult 0; col < tableView->model()->columnCount(); ++col) {
QModelIndex index = tableView->model()->index(row, col);
+= "1";
}
}
sBoxResult1 = applySBox(S_BOX1, xorResult.substr(0, 4 QVariant data = tableView->model()->data(index, Qt::DisplayRole);
standardModel->setData(standardModel->index(row, col));
sBoxResult2 = applySBox(S_BOX2, xorResult.substr(4, 4));
sBoxOutput = decimal), data);
}
}
// 将QStandardItemModel设置为QTableWidget的模型
tableWidget->setModel(standardModel);
```
这样就可以将QTableView中的数据显示在QTableWidget中了。注意,该方法只适用于将ToBinary(sBoxResult1, 2) + decimalToBinary(sBoxResult2, 2);
p4Output = applyPQTableView的数据显示在QTableWidget中,无法保留QTableView的其他特性,如样式、编辑功能等。