#include <iostream>
#include <string>
usingnamespace 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;
}
To expand on that a little - in a cpp file, it's something you should be careful about. There's nothing wrong with using usingnamespace 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.