Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between std:string and std::string [duplicate]

Tags:

In one function of my code I found a bug. It was written std:string :

const std::string currentDateTime() {     time_t     now = time(0);     struct tm  tstruct;     char       buf[80];     tstruct = *localtime(&now);     //strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);     strftime(buf, sizeof(buf), "%Y%m%d%X", &tstruct);      std:string str = buf;      str.erase(std::remove(str.begin(), str.end(), ':'), str.end());     return str; } 

The code compiles without errors. Why does it compile? What does std:string mean then?

like image 391
vico Avatar asked Oct 09 '15 12:10

vico


People also ask

What is the difference between string and std::string?

Differences between std::string and String Pavan. std::string is the string class from the standard C++ library. String is some other string class from some other library. It's hard to say from which library, because there are many different libraries that have their own class called String.

What is the difference between std::string and std :: vector?

std::string offers a very different and much expanded interface compared to std::vector<> . While the latter is just a boring old sequence of elements, the former is actually designed to represent a string and therefore offers an assortment of string-related convenience functions.

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.

How do you duplicate a string in C++?

To copy c-strings in C++, strcpy() function is used.


1 Answers

It is interpreted as a label that can be used with goto.

int main() {     //label to jump to:     label_name:     //some code     <..>     //some code     goto label_name;//jump to the line with the lable } 

Obviously that was a typo. Your code compiled because using namespace std; or using std::string was used somewhere above. Otherwise you'd get a "string was not declared in this scope" error.

like image 196
SingerOfTheFall Avatar answered Oct 27 '22 01:10

SingerOfTheFall