如何对1D双阵列进行倾斜
how to decleare a 1D double array
从键盘输入的数据。数组的长度可能很长。我想在按两次ENTER键时完成输入。用空格、制表符或","分隔的数字。如何检测长度n?我试过这样:
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
void main()
{
bool flag=true;
unsigned n=0,i;
double x;
string line,str;
istringstream iss;
cout<<"input your numbers."<<endl;
count<<"Press the Enter key twice finish data inputting."<<endl;
while(flag)
{
getline(cin,line);
str+=line+' ';
if(line.empty())
flag=false;
}
// get the length n
iss.str(str);
while(iss>>x)
{
n++;
}
double *v=new double[n];
iss.seekg(0);
for(i=0;i<n;i++)
iss>>v[i];
for(i=0;i<n;i++)
{
cout<<v[i];
if(i<n-1)
cout<<' ';
}
cout<<endl;
delete []v;
}
我是个新手。帮帮我,求你了!
Try this After taking the input in variable line do this:
#include<iostream>
#include<string>
#include<sstring>
using namespace std;
void main()
{
bool flag=true;
unsigned n,i=0;
double x;
string line,str;
istringstream iss;
cout<<"input your "
getline(cin,line);
int c=0;
char * pch;
pch = strtok (line," ,");// this will work for space and comma but you can add your own specifiers
while (pch != NULL)
{
printf ("%sn",pch);
pch = strtok (NULL, " ,");
c++;
}
return(c);// will give the length of arry.
}
人工声明1D数组语法是
void main()
{
int array[5];
//to initalize;
for(int i=0;i<5;i++)
{
array[i]=i;
}
for(int i=0;i<5;i++)
{
cout<<array[i]<<" ";
}
// and to declare dynamically
int * array=new int[5];
}
我已经解决了这个问题。
#include<iostream>
#include<string>
#include<sstream>
using namespace std;
void main()
{
bool flag=true;
unsigned n=0,i;
double x;
string line,str;
istringstream iss;
cout<<"input your numbers."<<endl;
cout<<"Press the Enter key twice finish data inputting."<<endl;
//Brecause data may come from clipboard and have multi line.
while(flag)
{
getline(cin,line);
str+=line+'n';
// vc++ 6.0 have a bug in include file: STRING. You shoud fix it.
//Or you shoud press ENTER more than twice to terminate inputing.
//Replace I.rdbuf()->snextc(); to _I.rdbuf()->sbumpc();
if(line.empty())
flag=false;
}
// get the length n
iss.str(str);
while(iss>>x)
{
n++;
}
double *v=new double[n];
iss.clear();// very important.
// initialize v[n]
iss.str(str);
for(i=0;i<n;i++)
iss>>v[i];
// output v
for(i=0;i<n;i++)
{
cout<<v[i];
if(i<n-1)
cout<<' ';
}
cout<<endl;
delete []v;
}
相关文章:
- OpenMP阵列性能较差
- 如何将三维尺寸不固定的三维阵列展平为一维阵列
- 当我的阵列太大时出现分段错误
- 位阵列上的快速AND运算
- 阵列必须使用大括号封闭的初始器进行初始化
- 没有从阵列<float>到阵列<int>的可行转换
- C++动态安全 2D 交错阵列
- 将平面阵列重塑为复杂的特征类型
- 如何使用英特尔 PIN 捕获阵列的所有负载?
- 为什么我能够为阵列分配比计算机实际拥有的内存更多的内存
- 库特<<恩德尔;不适用于打印 2D 阵列
- 数组类 阵列的打印输出
- 从较小的阵列到较大的阵列的元素级转换
- 仅在大型阵列上出现合并排序分段错误
- 从阵列C++中删除重复项
- 阵列的大小在C++中是否灵活?
- 反转阵列中的正序列
- 具有可变尺寸的 C++ 2D 阵列
- CUFFT:当输入是倾斜阵列时,如何计算fft
- 如何对1D双阵列进行倾斜