如何对1D双阵列进行倾斜

how to decleare a 1D double array

本文关键字:倾斜 阵列 1D      更新时间:2023-10-16

从键盘输入的数据。数组的长度可能很长。我想在按两次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;
}