现代c++读取文本文件,文本文件:"s.txt"中有字符串,如:"{2,4,3,1}",获取数字如:2,4,3,1赋值给变量 vectov<int>La;
#include <iostream>
#include <vector>
#include <fstream>
//#include <sstream>
#include <random>
#include <Eigen/Dense> //Eigen::MatrixXd forwardPropagation(const std::vector<Eigen::MatrixXd>
using namespace std;
/*
std::vector<int> parseBracketContent(const std::string& line) {
std::vector<int> result;
// 找到大括号的位置
std::size_t start_pos = line.find('{');
std::size_t end_pos = line.find('}');
// 确保找到合法的大括号
if (start_pos == std::string::npos || end_pos == std::string::npos || start_pos >= end_pos) {
return result; // 返回空的vector
}
// 截取大括号之间的内容
std::string content = line.substr(start_pos + 1, end_pos - start_pos - 1);
// 使用istringstream和getline进行分割
std::istringstream ss(content);
std::string token;
while (std::getline(ss, token, ',')) {
// 使用stoi将字符串转换为整数,并添加到结果vector中
result.push_back(std::stoi(token));
}
return result;
}//std::vector<int> parseBracketContent( */
std::vector<int> readLayers(const std::string& filename) {
std::ifstream file(filename);
std::string line;
std::vector<int> layers;
std::getline(file, line);
//------------------------------------------
// 找到大括号的位置
std::size_t start_pos = line.find('{');
std::size_t end_pos = line.find('}');
// 确保找到合法的大括号
if (start_pos == std::string::npos || end_pos == std::string::npos || start_pos >= end_pos) {
cout << "没有大括号!" << endl;
// return result; // 返回空的vector
}
// 截取大括号之间的内容
std::string content = line.substr(start_pos + 1, end_pos - start_pos - 1);
cout <<"Line55:" << content << endl;
// 使用istringstream和getline进行分割
std::istringstream ss(content);
std::string token;
while (std::getline(ss, token, ',')) {
// 使用stoi将字符串转换为整数,并添加到结果vector中
layers.push_back(std::stoi(token));
cout << std::stoi(token) << std::endl;
}
// std::cout << layers << std::endl;
//-------------------------------------------
// if (file.is_open()) {
std::getline(file, line);
std::istringstream iss(line);
int num;
while (iss >> num) {
// layers.push_back(num);
}
// }//if (file.is_open()) {
return layers;
}//std::vector<int> readLayers(
Eigen::MatrixXd forwardPropagation(const std::vector<Eigen::MatrixXd>& weights, const Eigen::VectorXd& input) {
Eigen::MatrixXd output = input;
for (const auto& w : weights) {
output.conservativeResize(output.rows(), 1); // Make sure it's a column vector
output = w * output;
output = output.unaryExpr([](double x) { return 1.0 / (1.0 + std::exp(-x)); }); // Activation function (sigmoid)
}
return output;
}
int main() {
// Read network architecture from file
std::vector<int> layers = readLayers("\\s.txt");
// Initialize weights randomly
std::default_random_engine generator;
std::normal_distribution<double> distribution(0.0, 1.0);
std::vector<Eigen::MatrixXd> weights;
for (size_t i = 0; i < layers.size() - 1; ++i) {
Eigen::MatrixXd w(layers[i + 1], layers[i]);
w = w.unaryExpr([&](double x) { return distribution(generator); });
weights.push_back(w);
}
// Initialize input (example)
Eigen::VectorXd input(layers[0]);
input << 0.5, 0.6;
// Perform forward propagation
Eigen::MatrixXd output = forwardPropagation(weights, input);
std::cout << "Output: \n" << output << std::endl;
return 0;
}//main