STUCK WITH #INCLUDE <STRING>

Pages: 123
Oct 17, 2010 at 2:35pm
Hi, i am learning from a book, and in one section it says:

"I include the file string, part of the standard library, so i can use a string object to access a string through a variable"

I have not idea what that means

what does it do by including the string header file?

Can anyone explain in simpler terms please.
Oct 17, 2010 at 2:43pm
I think it's saying that you should use the library string..

#include <string>

it lets you get access to strings and manipulate it..
Oct 17, 2010 at 2:45pm
thanks for the reply, can you give me an example of when you would put the file string to use?
Oct 17, 2010 at 3:01pm
what is a string object, and how are they used?
Oct 17, 2010 at 3:07pm
Ah, the std::string class!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>

int main()
{
    std::string newstring;
    newstring = "Here is some text";
    newstring += " in the string."; //newstring now contains: Here is some text in the string.
    newstring == "Here is some text in the string."; //This evaluates to true.
    newstring = "Read: http://cplusplus.com/reference/string/string/";
    std::cout << newstring << std::endl;
    std::cin.ignore();
    return 0;
}

//EDIT: Bloody unawareness in the morning.
//-Albatross 
Last edited on Oct 17, 2010 at 3:13pm
Oct 17, 2010 at 3:18pm
hmm so,

by including the string file am i basicaly including another type of variable... which is a type which holds sentances and words???

would be right in thinking its a type of variable because my book doesnt call it a variable, it calls it an object from what i get from it?
Last edited on Oct 17, 2010 at 3:21pm
Oct 17, 2010 at 3:21pm
That's right. It's a type.

-Albatross
Oct 17, 2010 at 3:22pm
Ok, thanks for clearing that up, one last thing, why does my book call it an object instead of a variable type?
Oct 17, 2010 at 3:30pm
ahh it dont matter, i understand :)
Oct 17, 2010 at 3:40pm
Actually, one last thing.

If i have to include a file to use the string variable, why dont i have to include files for other variables such as int and char?
Oct 17, 2010 at 3:43pm
Those types are built into C++. std::string, however, is not built in. It was added on in a separate library after C++ was created (AFAIK).

-Albatross
Last edited on Oct 17, 2010 at 3:43pm
Oct 17, 2010 at 3:47pm
Much appreciated
Oct 17, 2010 at 3:52pm
// I know you may already understand it, but I'd like to try an answer ^.^

// Including <string> let's you use std::string insertNameHerein the same way you need to include <iostream> in order to use std::cout and std::cin!

//By the way, the terminology for everything really confuses me as well.
Oct 17, 2010 at 4:01pm
Thanks :),

But the only difference is that by including the iostream file you include cout and cin which are input and output objects, and whereas by including the string file you are including a variable type.

Right???
Oct 17, 2010 at 4:25pm
// *cry* I have no idea!...Must... have... MOAR... knowledge...

// Thank you actually, if you're right, I didn't know they were different in that way(whatever way "that" way is lol). Wait...I think I get what you're saying, since they're both used differently. So an object is something that....uh.. [insert easy description here]... okay well after "cout" you use the "<<".. thing... but for a string you say "string" before you(declare?define?other?) invent a name for...it?.. Uh.... how do I learn these things? ^.^

// Okay so wait, cout and cin are objects? So if string is a "variable type" does that mean if I said, say: std::string aStringyName std is, something, :: is the scope resolution..operator? Anyway so "string" is a variable type, and the name you give it would be called a variable? Or, a variable name? It's these kinds of things I wish books would focus a bit more on teaching, what things are called X.x

// Er... basically what I'm trying to say is "If what you say is true, thank you, for you have taught me more about the terminology." lol
Last edited on Oct 17, 2010 at 4:26pm
Oct 17, 2010 at 4:32pm
Hahaa :D

I think im right!

Can anyone confirm this for us?
Oct 17, 2010 at 4:52pm
The IOstream library also defines its own types (fstream, sstream, ostream, istream) and the string library also defines it's own functions (getline()), and then some, but that's just FYI.

I'm not the strongest regarding terminology, however I have been programming in C++ for a few years and maybe this can help you:

● An object can be an instance of a class.
● A class is a type of type, if you will. An std::string is a class. If you have something in C++ that can do something along the lines of something.something_else..., it's safe to assume the left-hand side is a class (or a struct, but in C++ the two are pretty much the same).
● The term variable is usually reserved for instances of simpler types than classes (int, char, float).
● Well... okay. Technically std::string is a typedef of a class template using a char, but you won't worry about that later. ;)

Do these help you understand a bit? You might want to check the tutorial on this site to help you out (http://cplusplus.com/doc/tutorial/).

-Albatross
Last edited on Oct 17, 2010 at 4:53pm
Oct 17, 2010 at 5:08pm
I started to read abit of that and i got abit more confused so im not going to read it lol
But thats, i do appreciate it.

can you just say whether me and Nohbdy are right in what we think

:D

So, if i include the string file, and i type "string leader"

Am i making a variable called leader in which it can holder multiple letters?
Last edited on Oct 17, 2010 at 5:09pm
Oct 17, 2010 at 5:54pm
Assuming I understood you, you seem to be more or less right. std::cin std::and cout are objects, while std::string is a type.

If you include the string file, and you're either using namespace std; or using std::string;, then when you type string leader; you are creating an object whose purpose is to hold... a string of characters, hence the name, hehe. There's an upper limit to how many it can hold, but it's pretty high.

The std::string class also has several functions within it to replace the functions in the cstring library, one of my favorite being find().
1
2
3
std::string naem;
naem = "Can we get some aardvarks up in this program!?!";
unsigned int pos = naem.find("aardvark"); //Returns the position of the first instance of the string "aardvark". 


See the reference I gave you earlier for more details.

Happy coding! :)

-Albatross
Oct 17, 2010 at 6:27pm
ok

when you say "you are creating an object whose purpose is to hold"

is the word object the technical word to use, is it not called a variable of the string type?
Last edited on Oct 17, 2010 at 6:37pm
Pages: 123