Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::string vs string in c++ [duplicate]

Tags:

c++

string

std

Possible Duplicates:
Why is 'using namespace std;' considered a bad practice in C++?
Using std Namespace

Ive been hovering around a bunch of different forums and i seem to see this pop up every time and again. Its a very much beginner question.

I usually define a program with

#include<string> using namespace std;  string x; 

I see a bunch of code samples out there who define a string as

std::string. 

What is the purpose of this? is it good practice or have some functionality?

like image 892
OVERTONE Avatar asked Mar 31 '11 11:03

OVERTONE


People also ask

Are C-strings faster than std::string?

C-strings are usually faster, because they do not call malloc/new. But there are cases where std::string is faster. Function strlen() is O(N), but std::string::size() is O(1). Also when you search for substring, in C strings you need to check for '\0' on every cycle, in std::string - you don't.

Is std::string the same as string?

There is no functionality difference between string and std::string because they're the same type.

Does std::string make a copy?

It's called "deep copy". If only the pointer itself was copied and not the memory contents, it would be called "shallow copy". To reiterate: std::string performs deep copy in this case.

Should I use std::string or * char?

Use std::string when you need to store a value. Use const char * when you want maximum flexibility, as almost everything can be easily converted to or from one.

What is the difference between string and string in C #?

C#. By TutorialsTeacher. 16 Jan 2020. Essentially, there is no difference between string and String (capital S) in C#. String (capital S) is a class in the .NET framework in the System namespace. The fully qualified name is System.String. Whereas, the lower case string is an alias of System.String. Consider the following example.

Is it necessary to use std::string instead of STD?

As the other answer already stated, using std:: is necessary unless you import either the whole std namespace or std::string (see below). In my opinion it's nicer to use std::string instead of string as it explicitly shows that it's a std::string and not some other string implementation.

What is string_view in C++?

A std::string_view can refer to both a C++ string or a C-string. All that std::string_view needs to store is a pointer to the character sequence and a length. std::string_view provides the same API that std::string does, so it is a perfect match for C-style string literals.

What is the full name of string in C++?

The full name of stringis std::stringbecause it resides in namespace std, the namespace in which all of the C++ standard library functions, classes, and objects reside. In your code, you've explicitly added the line using namespace std;, which lets you use anything from the standard namespace without using the std::prefix.


1 Answers

As the other answer already stated, using std:: is necessary unless you import either the whole std namespace or std::string (see below).

In my opinion it's nicer to use std::string instead of string as it explicitly shows that it's a std::string and not some other string implementation.

If you prefer to write just string though, I'd suggest you to use using std::string; instead of using namespace std; to only import the things into the global namespace that you actually require.

like image 62
ThiefMaster Avatar answered Oct 05 '22 06:10

ThiefMaster