I am trying to slice the last four characters off a character array, and I tried the method that Python uses without success;
char *charone = (char*)("I need the last four")
char *chartwo = charone[-4:]
cout << chartwo << endl;
I would want this code to return:
four
But C/C++ doesn't seem to be that easy...
开发者_运维问答Where could I find a simple alternative that will return the last four characters of one character array into another character array?
Try:
int len = strlen(charone);
char *chartwo = charone + (len < 4 ? 0 : len - 4);
In C++, you can replace that with:
char* chartwo = charone + (std::max)(strlen(charone), 4) - 4;
The code uses a special property of C strings that only works for chopping off the beginning of a string.
First, let's remove the deprecated conversion:
char const *charone = "I need the last four";
Arrays are not first-class values in C++, and they don't support slicing. However, just as the above charone points to the first item in the array, you can point to any other item. Pointers are used with chars to make C-style strings: the pointed-to char up until a null char is the contents of the string. Because the characters you want are at the end of the current (charone) string, you can point at the "f":
char const *chartwo = charone + 16;
Or, to handle arbitrary string values:
char const *charone = "from this arbitrary string value, I need the last four";
int charone_len = strlen(charone);
assert(charone_len >= 4); // Or other error-checking.
char const *chartwo = charone + charone_len - 4;
Or, because you're using C++:
std::string one = "from this arbitrary string value, I need the last four";
assert(one.size() >= 4); // Or other error-checking, since one.size() - 4
// might underflow (size_type is unsigned).
std::string two = one.substr(one.size() - 4);
// To mimic Python's [-4:] meaning "up to the last four":
std::string three = one.substr(one.size() < 4 ? 0 : one.size() - 4);
// E.g. if one == "ab", then three == "ab".
In particular, note that std::string gives you distinct values, so modifying either string doesn't modify the other as happens with pointers.
C++ and Python are very different. C++ does not have built-in Python-like string facilities, but its Standard Template Library has a handy std::string
type, which you should look into.
If you are looking to just briefly access the last four characters, then something like
char* chartwo = charone + (strlen(charone) - 4);
will be fine (plus some error checking).
But if you want to replicate the python functionality, you will need to copy the last four characters. Again, use strlen to get the length (or store it somewhere beforehand), and then use strcpy (or probably a better stl function that has the same function). something like...
char chartwo[5];
strcpy(chartwo, charone + strlen(charone) - 4);
(Note: If you don't copy, then you can't free charone until you are finished using chartwo. Also, If you don't copy, then if you change charone later, chartwo will change as well. If that's okay, then sure, just point to the offset.)
This will do it:
char* chartwo = charone + 16;
array slicing in c++:
array<char, 13> msg = {"Hello world!"};
array<char, 6> part = {"world"};
// this line generates no instructions and does not copy data
// It just tells the compiler how to interpret the bits
array<char, 5>& myslice = *reinterpret_cast<array<char,5>*>(&msg[6]);
// now they are the same length and we can compare them
if( myslice == part )
cout<< "huzzah";
This is just one of the emamples where slicing is usefull
I have made a small library which does this with compile-time bounds checks at https://github.com/Erikvv/array-slicing-cpp
精彩评论