I tried to print the address of each character std::string . But I donโt understand what is happening inside with std::string , which leads to this conclusion, and for the array it gives the address, as I expected. Can someone explain what is happening?
std::string
#include <iostream> #include <string> using namespace std; int main(){ string str = "Hello"; int a[] = {1,2,3,4,5}; for( int i=0; i<str.length(); ++i ) cout << &str[i] << endl; cout << "**************" << endl; for( int i=0; i<5; ++i ) cout << &a[i] << endl; return 0; }
Conclusion:
Hello ello llo lo o ************** 0x7fff5fbff950 0x7fff5fbff954 0x7fff5fbff958 0x7fff5fbff95c 0x7fff5fbff960
When a std::ostream tries to print a char* , it takes a C-style string.
std::ostream
char*
Before printing, put it before void* , and you get what you expect:
void*
cout << (void*) &str[i] << endl;
or you can use old printf
printf("\n%x",&s[i]);
Source: https://habr.com/ru/post/897447/More articles:Is there any way to apply TDD methods for dev in PL / SQL - databasehttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/897443/what-is-the-correct-usage-of-cmake-externalprojectadd-with-a-git-repository&usg=ALkJrhgQHLfi5VIEf0RC5sJMRMfPGxoF8ARecover from Core Data error in saveContext - ioshttps://translate.googleusercontent.com/translate_c?depth=1&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/897445/dialog-asking-if-user-wants-to-allow-use-of-location-services-pops-and-disappears-in-iphone&usg=ALkJrhghLW9xLT2-4iQOJXjvl4SuhAsM_wScrollViewer steals focus - wpfCopy files matching wildcard combinations recursively, but donโt create a directory tree in DOS - dosIs it possible to dynamically remove the meta refresh tag from the header using jQuery? - jqueryType .GetType (string typeName) returns null - reflectionCucumbers and rspec share factory girls factory - ruby-on-railsHow to detect missing ink - windowsAll Articles