-
Reading in vectors
Can anyone tell me how to read in a string into a vector? I know that if I use cin (as I did below) it will not read whitespaces. But if I use cin.getline, cin.get or gets it gives me an error because of the subscript (which I need) and if I take it out, it tells me that my members are not part of my vector!
I've had this problem before but for some reason I always draw a blank when I encounter it. Can anybody point me in the right direction?
Code:
void FullName(vector<Names>& Access, vector<Entry>& Process, int& i)
// Pre condition: User has entered A in the menu
// Post condition: will return the full name to the Add function
{
cout << "\nYou have chosen to add an entry.\n\n";
cout << "Please enter the last name: ";
cin >> Access[i].lastName;
cout << "\n\nPlease enter the first name: ";
cin >> Access[i].firstName;
cout << "\n\nPlease enter the middle initial: ";
cin >> Access[i].middleIni;
Process[i].fullName= Access[i].lastName+ " " +
Access[i].firstName+ " " +
Access[i].middleIni;
Add(Access, Process, i);
} // End FullName function
-
vector
why not just write..?
Code:
string s;
cin.getline(s,50);
access.insert(x, s);
if u want to do it the way u r trying to.. why not just use an array?
if u want an array with undefined length create it dynamically and reallocate memory.. or..
Code:
int *ptr;
cout << "Insert array size: ";
cin >> size;
ptr = new int[size];
telling u this because with vectors i don't believe u can write
acces[5] = "bob";
though i am not positive as i just used vectors for first time yesterday.. good luck
-
vector <string> names;
//names has a default capacity of unknown quantity and size 0.
string name;
cout << "enter your first and last names" << endl;
getline(cin, name);
//getline is overloaded for use with STL strings. The syntax is
//slightly different than for Cstyle strings.
names.push_back(name);
//push_back() a common mechanism to add to the vector, though
//there are others
-
yep many others...
perhaps this may be of help..
Code:
---------------------
iterator
insert(iterator position, const T& x);
Inserts x before position. The return value points to the inserted x.
------------------
void
insert(iterator position, size_type n, const T& x);
Inserts n copies of x before position.
-----------------------------
void
insert(iterator position, InputIterator first,
InputIterator last);
Inserts copies of the elements in the range [first, last] before position.
--------------------------
void
push_back(const T& x);
Inserts a copy of x to the end of self.
just some vector functions for inserting stuff.. there are more but i don't want to make this thread too huge.. just look in .h file for the rest mate...