comparing strings and std::

I'm to understand the use of the std::string str1 ("green apple"); assignment in the second set of code.

The first set of code is a simple routine that builds an array of strings.

The second set of code I got off of this web site.

What exactly does the std::string str1 ("green apple"); syntax do?

Why is "std::" also used with cout (std::cout)

std:: ,does what?

Thank you.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <string>

using namespace std;

int main ()
{
	string s ;
	s = "AAA";

  string dayName[10];
  dayName[0] = "Sunday";
  dayName[1] = "Monday";
  dayName[2] = "Tuesday";
  dayName[3] = "Wednesday";
  dayName[4] = "Thursday";
  dayName[5] = "Friday";
  dayName[6] = "Saturday";
  dayName[7] = "AAA";
  int stringLength = dayName[6].length();
 cout<< "The length of dayNAme[6] is "<< stringLength<<endl;
cout<<endl;
 if (dayName[7].compare(s) == 0) cout << " THEY ARE EQUAL "<< endl;



   return 0;
}

second set of code:
// comparing apples with apples
#include <iostream>
#include <string>

int main ()
{
  std::string str1 ("green apple");
  std::string str2 ("red apple");

  if (str1.compare(str2) != 0)
    std::cout << str1 << " is not " << str2 << '\n';

  if (str1.compare(6,5,"apple") == 0)
    std::cout << "still, " << str1 << " is an apple\n";

  if (str2.compare(str2.size()-5,5,"apple") == 0)
    std::cout << "and " << str2 << " is also an apple\n";

  if (str1.compare(6,5,str2,4,5) == 0)
    std::cout << "therefore, both are apples\n";

  return 0;
}


Last edited on
> What exactly does the std::string str1 ("green apple"); syntax do?

It initialises the object identified by str1 with the expression "green apple". This kind of initialisation is called direct initialisation.

1
2
3
std::string str1 ("green apple"); // direct initialisation
std::string str2 {"red apple"}; // direct initialisation
std::string str3 = "yellow apple"; // copy initialisation 



> std:: does what?

See http://www.cplusplus.com/forum/beginner/142171/#msg750694
Thank you for the reply. I browsed the the above link. Good post.
Another question?
Is using :
using namespace std;
a bad habit?

Is it better to use
std:: in your code?
example:
std::string s ; to declare a string in my first example.

Thank you.
Last edited on
using namespace std;
a bad habit?

Generally, yeah it's a bad habit to get into. IMO, using namespace:: everywhere is the best approach.
To expand on that a little - in a cpp file, it's something you should be careful about. There's nothing wrong with using using namespace std if you completely understand the implications of doing it, and are confident that pulling the entirety of std into the global namespace will cause no problems.

In a header file, it's definitely a bad thing to do. In fact, you should avoid using statements generally in headers.
Last edited on
Thanks you very much!
First time on this forum. Good stuff.
Topic archived. No new replies allowed.