我想知道是否有人可以给我一只手试图建立一个程序,从csv文件中读取大小未知的浮动数据块.我已经在MATLAB中写了这个,但是想要编译和分发这个,所以转向c ++.
我只是学习并尝试阅读这个开始
7,5,1989
2,4,2312
从文本文件.
代码到目前为止.
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <stdlib.h>
const int ROWS = 2;
const int COLS = 3;
const int BUFFSIZE = 80;
int main() {
int array[ROWS][COLS];
char buff[BUFFSIZE];
std::ifstream file( "textread.csv" );
std::string line;
int col = 0;
int row = 0;
while( std::getline( file, line ) )
{
std::istringstream iss( line );
std::string result;
while( std::getline( iss, result, ',' ) )
{
array[row][col] = atoi( result.c_str() …
Run Code Online (Sandbox Code Playgroud) 所以这是一个非常基本的问题和超级微不足道的但我只是通过c ++中的编程原则和实践,我的程序用于读取字符串和int的行为与Bjarne Stroustrup编写的书不同,所以如果他感到惊讶犯了一个错误.无论如何这里是代码:
#include "..\std_lib_facilities.h"
int main()
{
cout << "Please enter your first name and age\n";
string first_name = "???"; // string variable
// ("???” means “don’t know the name”)
int age = -1; // integer variable (1 means “don’t know the age”)
cin >> first_name >> age; // read a string followed by an integer
cout << "Hello, " << first_name << " (age " << age << ")\n";
}
Run Code Online (Sandbox Code Playgroud)
当我在提示符输入"22 Carlos"时它输出"Hello,22(0岁)",基本上使我的错误检查的初始化值无用.这是c ++或其他东西的新功能,这就是为什么这本书出错了?
编辑1:BTW我在Windows 7上使用GCC for cygwin,并且-std = …
我编写了一个程序,它读入带有单元顶点坐标的ascii文件,然后输出一个带有单元格中心的文件.有什么方法可以加快c ++代码,至少匹配我的matlab性能.我在我的c ++代码中使用push_back向量,但我不知道如何实现与数组类似?
我试图使用困难进行性能分析,但是如何在设置程序之前开始进行性能分析,它只能编程已经运行的运行,输出也很混乱,不要去我的代码行但是像std这样的行:: _ Vector_cal> ....等.什么是独占和包容的区别,最高的%独占名称是运营商删除,第二是运营商新????
我的c ++来源:
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
#include <cstdlib>
std::vector<double> GetValues_n(const std::vector<std::string>& src, int start, int end)
{
std::vector<double> ret;
for(int i = start; i <= end; ++i)
{
ret.push_back(std::strtod(src[i].c_str(), nullptr));
}
return ret;
}
std::vector<int> GetValues_c(const std::vector<std::string>& src, int start, int end)
{
std::vector<int> ret;
for(int i = start; i <= end; ++i)
{
ret.push_back(std::atoi(src[i].c_str()));
}
return ret;
}
std::vector<double> polycentre(const std::vector<double>& x,const …
Run Code Online (Sandbox Code Playgroud) 我想知道是否有人可以帮助我尝试构建一个程序,以从csv文件读取大小未知的浮点大数据块。我已经在MATLAB中编写了此代码,但希望对其进行编译和分发,因此转向c ++。
我只是学习并尝试阅读以开始
7,5,1989
2,4,2312
Run Code Online (Sandbox Code Playgroud)
从文本文件。
到目前为止的代码。
// Read in CSV
//
// Alex Byasse
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
#include <stdlib.h>
int main() {
unsigned int number_of_lines = 0;
FILE *infile = fopen("textread.csv", "r");
int ch;
int c = 0;
bool tmp = true;
while (EOF != (ch=getc(infile))){
if(',' == ch){
++c;
}
if ('\n' == ch){
if (tmp){
int X = c;
tmp = false;
}
++number_of_lines;
}
}
fclose(infile);
std::ifstream file( "textread.csv" …
Run Code Online (Sandbox Code Playgroud) 嘿我得到一个错误我认为与从阅读其他帖子复制流变量有关,我试图改变
std::ofstream outfil;
Run Code Online (Sandbox Code Playgroud)
至
std::ofstream & outfil;
Run Code Online (Sandbox Code Playgroud)
但我得到错误
reference value "outfil" requires an initializer
Run Code Online (Sandbox Code Playgroud)
代码是:
#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
#include <cstdlib>
std::vector<double> GetValues_n(const std::vector<std::string>& src, int start, int end)
{
std::vector<double> ret;
for(int i = start; i <= end; ++i)
{
ret.push_back(std::strtod(src[i].c_str(), nullptr));
}
return ret;
}
std::vector<int> GetValues_c(const std::vector<std::string>& src, int start, int end)
{
std::vector<int> ret;
for(int i = start; i <= end; ++i)
{
ret.push_back(std::atoi(src[i].c_str()));
}
return …
Run Code Online (Sandbox Code Playgroud) 使用-g命令编译的任何程序是否都有可供gbd列出的源代码,即使源代码文件不可用?另外,当您在具有复杂多源文件结构的程序中的某一行设置断点时,您是否需要源代码文件的名称?