I'm curious as to the internal workings of strings as it relates to input. When a user types into a program and the input goes directly into a string std::cin >> input // assume input is string , those characters are first typed into the program using the keyboard, put into the input buffer and then extracted from the buffer, using operator>>() (or some variation, I asssume) , into the internal character array in the string object; but how do strings allocate just enough space for the internal character array before the length of the input, in the input buffer, is known. Do they extract each character, one by one, from the input buffer and incrementally rebuild the character array until the sentinal character is found?
I should clarify i know the general answer: dynamic arrays. I'm looking for more depth, specifically as it relates to retrieving items of unknown lengths directly from the input buffer and creating accommodating space.
What you're asking is very unclear to me. Are you trying to ask how you can create your own string object independent from the c++ created string object?
#include<iostream> //lib for cout
#include<stdio.h> //lib for gets()
#include<conio.h> //lib for getch()
usingnamespace std;
int main()
{
char name[200]; //200 is the lengh of the text "name"
cout<<"Enter your full name: ";
gets(name);
cout<<"Your name is: "<<name;
getch(); //i put this beacuse my compiler doesn't put a break. xDD
return 0;
}
how do strings allocate just enough space for the internal character array
They don't. Strings allocate a predefined amount of space. They use what they need, and if they need more space they allocate more. The string class has a size method somestring.size(); which returns the number of characters in the string, but the string class also has a capacity method somestring.capacity(); which returns the amount of space allocated in bytes. These two methods do not have to return the same number.