I know that we use #include<iostream> for cout function.
However, I'm curious when we need to use #include<string>.
Im a student taking C++ class.
Thanks.
Include it whenever you need something from that header. Here you can find a list of things that you need to #include <string> for.
Related
I'm trying to learn about strings, but different sources tell my to include different headers.
Some say to use <string.h>, but others mention "apstring.h". I was able to do some basic stuff with apstring, but I've been told the other one is more powerful. When I include <string.h> and try to declare some string variables, however, I get errors. What is the proper usage?
You want to include <string> and use std::string:
#include <string>
#include <iostream>
int main()
{
std::string s = "a string";
std::cout << s << std::endl;
}
But what you really need to do is get an introductory level book. You aren't going to learn properly any other way, certainly not scrapping for information online.
Sources telling you to use apstring.h are materials for the Advanced Placement course in computer science. It describes a string class that you'll use through the course, and some of the exam questions may refer to it and expect you to be moderately familiar with it. Unless you're enrolled in that class or studying to take that exam, ignore those sources.
Sources telling you to use string.h are either not really talking about C++, or are severely outdated. You should probably ignore them, too. That header is for the C functions for manipulating null-terminated arrays of characters, also known as C-style strings.
In C++, you should use the string header. Write #include <string> at the top of your file. When you declare a variable, the type is string, and it's in the std namespace, so its full name is std::string. You can avoid having to write the namespace portion of that name all the time by following the example of lots of introductory texts and saying using namespace std at the top of the C++ source files (but generally not at the top of any header files you might write).
I don't hear about "apstring".If you want to use string with c++ ,you can do like this:
#include<string>
using namespace std;
int main()
{
string str;
cin>>str;
cout<<str;
...
return 0;
}
I hope this can avail
You shouldn't be using string.h if you're coding in C++. Strings in C++ are of the std::string variety which is a lot easier to use than then old C-style "strings". Use:
#include <string>
to get the correct information and something std::string s to declare one. The many wonderful ways you can use std::string can be seen here.
If you have a look at the large number of questions on Stack Overflow regarding the use of C strings, you'll see why you should avoid them where possible :-)
The C++ string class is std::string. To use it you need to include the <string> header.
For the fundamentals of how to use std::string, you'll want to consult a good introductory C++ book.
Maybe this link will help you.
See: std::string documentation.
#include <string> is the most widely accepted.
"apstring" is not standard C++, in C++, you'd want to #include the <string> header.
Use this:
#include <string>
For using the string header first we must have include string header file as #include <string> and then we can include string header in the following ways in C++:
1)
string header = "--- Demonstrates Unformatted Input ---";
2)
string header("**** Counts words****\n"), prompt("Enter a text and terminate"
" with a period and return:"), line( 60, '-'), text;
I am having issues with my game. I haven't made much of a game so far, but I am trying to call a string using a external function from my bin class. When I compile, It says I can't have non-static variables.
#include <iostream>
#include <stdlib.h>
using namespace std;
class Bin {
string gameStart = "How would you like to start?\n";
};
Bin bin1;
int main () {
cout << bin1.gameStart;
}
Just for reference, I have tried looking for solutions, but to no prevail.
Update: Hi again. I have read some of the feedback from my question. I see that Raw N and Angew have made some valid points. I would like to ask if these two nice people: Where does the header go. How can i get that version of the compiler?
Thanks!
Update: I managed to find out how to compile my code in c++14. There is a console command that can be used for this. I updated my open.bat file, which i use to compile my notepad code. Everything is working fine now. Thanks all!
The default for a class' members is private, so without specifying differently, your data will not be accessible from outside the class.
Add public: inside the class, before the declarations.
I have a header file. In this header I want to use a map for a class. But after I include I get a no matching function error, because I have a copy() function in the project(really big project). I saw on this website http://www.sgi.com/tech/stl/download.html that map contains a using std::copy so I guess it collides with that.
I cannot make any changes to the copy function, so is there a way I can use map in this header file ? (no other place). Is there a way so my copy functions don't collide ?
I use Visual Studio 2008 on Windows 7.
The error message suggests that your function is not visible to the translation unit, so make sure you include the header.
Also, I doubt <map> has anything like using std::copy. You sure about this one?
In algobase.h (which is not standard C++), the using directives are parsed only if __STL_USE_NAMESPACES is defined. You should undefine it before including the header:
#undef __STL_USE_NAMESPACES
#include "algobase.h"
#endif
You should be able to wrape the map include in a namespace i.e.
namespace StopCollision
{ #include <map> }
Then the map copy function would be available via StopCollision::std::copy. This is very crude, but would solve your problem.
To the best of my knowledge the map stl class doesn't have a copy function. It is probably an include issue, you could try using a forward reference to the class that contains the map and copy function.
I am working on a very big and old C++ project. The word "vector" has been used all over the place. I am now trying to add new functionality to the project utilizing STL vector. This is not working. In addition, I am only allowed to modify specific sections of the code, so I can not change their use of "vector".
Is there a way to rename STL vector and use it?
Use C++ namespaces.
#include <vector>
// note the absence of `using namespace std;` line
int main() {
// note the `std::` qualification
std::vector<int> x;
// ...
}
As long as you don't put using namespace std; anywhere (which is not a great thing to do anyway), there will be no conflict between ::std::vector and any other vector.
I'm assuming that this infinite wisdom didn't extend to declaring names inside std; in that case, my best advice is to run away. I'm also assuming that you're talking about the modern C++ library, not the STL which (I think) didn't have its own namespace.
Remove the using namespace std; from the beginning of the file, and add std:: in the code wherever is needed:
std::cout << "debug" << std::endl;
std::vector<std::string> simple_vector;
Just refer to it using the full name std::vector.
In C++11, you could also use a using statement.
Don't do this.
#define vector stdVector
#include <vector>
#undef vector
End of don't do this
You can either use namespaces to qualify your version of vector or the one from std, after you remove the using directives.
Simple question, how do I shorten a call/name without using defines.
For example, I have a singleton that I have to call that is within a namespace (I cannot use using namespace blabla because it is not allowed) like so:
MyFW::GameRoot::Instance()->DoSomething();
Now I can assign that to a variable, which works somewhat if I am using it multiple times within the same class/function, but using it in many classes/functions it becomes cumbersome. I decided to use #define for it:
#define MyFW::GameRoot::Instance() ROOT //defined in GameRoot.h
ROOT->DoSomething(); //Used where-ever GameRoot.h is included
Much better, and I really like it especially because now wherever I see ROOT (color coded through V-Assist) I know what it is immediately... unless I have a breakpoint there and I need Visual Studio to resolve ROOT to show up in the watch window (or even hover over it to quickly pull up the object in debug), which it cannot do.
Is there any other option? What do you guys do to shorten names? Simply use local/member pointers to store the instance?
Thanks!
You can't use using namespace ..., but can you use
namespace root=MyFW::GameRoot;
Then you can type
root::Instance()->DoSomething();
Defining a namespace like that is better than a #define. (I.e it can't get munged up somewhere else by mistake. The compiler knows what you are trying to do.)
Use local references:
MyFW::GameRoot& ROOT = *MyFW::GameRoot::Instance();
Do not use defines.
If you want to ease access across multiple functions, just use a helper function:
namespace {
MyFW::GameRoot* root() { return MyFW::GameRoot::Instance(); }
}
// ...
root()->DoSomething();
Two characters more, but it with comes type-safety included.
The good way to do this (but never in a header) is
using MyFW::GameRoot;
GameRoot::Instance()->DoSomething;
This is a using declaration and is different from a using directive, which is what you mentioned above.