我有 gcc (Cadence) 4.8.3。我想编译一个包含 string_view 的 C++ 文件。当我编译文件时:
g++ script.cpp
出现以下错误:
script.cpp:4:23: fatal error: string_view: No such file or directory
#include <string_view>
^
compilation terminated.
我尝试升级我的 gcc 版本,但失败了。
sudo apt-get install gcc-5
sudo: apt-get: command not found
我的问题是如何解决这个问题,如何升级我的 gcc 版本,或者是否有替代方案可以替换 string_view 库。
.cpp 脚本如下:
#include <iostream>
#include <string>
#include <map>
#include <string_view>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>
#include<cmath>
#include <fstream>
#include <string>
#include <sstream>
#include <iomanip>
#define FEATURE 27
double get_value(std::string_view value_str,double* smons) {
std::map<std::string, double *> arrays_names {
{"smons", smons}
};
auto name_end_pos = value_str.find('[');
auto name = value_str.substr(0, name_end_pos);
auto index_end_pos = value_str.find(']');
auto index_len = index_end_pos - name_end_pos - 1;
std::string index_str{value_str.substr(name_end_pos + 1, index_len)};
auto index = std::stoi(index_str);
return arrays_names[std::string(name)][index];
}
double evaluate_expression(const std::string& expr,double* smons) {
double a;
if (expr.size()==8)
{
a=get_value(expr.substr(0, 8),smons);
}
else if (expr.size()==9)
{
a=get_value(expr.substr(0, 9),smons);
}
else if (expr.size()==17)
{
a=get_value(expr.substr(0, 8),smons) * get_value(expr.substr(9, 8),smons);
}
else if (expr.size()==18)
{
a=get_value(expr.substr(0, 8),smons) * get_value(expr.substr(9, 9),smons);
}
else if (expr.size()==19)
{
a=get_value(expr.substr(0, 9),smons) * get_value(expr.substr(10, 9),smons);
}
return
a;
}