I enter a squencie of doubles in a vector but the last number is an EOS. How i dont enter that last number which stop the sequence?.
double vec[MAX];
int i = 0;
while(vec[i-1] != EOS){
cin >> vec[i];
i++;
}
You can use the following program to add the number from the sequence just read into the std::vector
.
#include <iostream>
#include <sstream>
#include <vector>
int main()
{
std::vector<double> vec; //or you can create the vector of a particular size using std::vector<double> vec(size);
double EOS = 65; //lets say this is the EOS
std::string sequence; //or you can use std::string sequence = "12 43 76 87 65";
std::getline(std::cin, sequence);//read the sequence of numbers
std::istringstream ss(sequence);
double temp;
while((ss >> temp) && (temp!=EOS))
{
vec.push_back(temp);
}
std::cout<<"elements of the above vector are: "<<std::endl;
//lets print out the elements of the vector
for(const double &elem: vec)
{
std::cout<<elem<<std::endl;
}
return 0;
}
The output of the above program is(for a given input shown below):
12 43 78 98 65
elements of the above vector are:
12
43
78
98
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.