开发者

I'm finding String::copy rather difficult: copy first five characters of a string

开发者 https://www.devze.com 2023-02-03 13:46 出处:网络
I\'m trying to teach myself to program, so I apologize in advance for any shoddy code or bad practices. Basically, I\'m trying to copy part of a long string using string::copy, but I\'m clearly not do

I'm trying to teach myself to program, so I apologize in advance for any shoddy code or bad practices. Basically, I'm trying to copy part of a long string using string::copy, but I'm clearly not doing something right. My goal here is to copy and print the first five characters of the string "bignumber":

#include <iostream>
#include<string>

using namespace std;

int main()
{
     const string bignumber = "73167176531330624919225119674426574742355349194934\
                               96983520312774506326239578318016984801869478851843\
                               85861560789112949495459501737958331952853208805511\
                               12540698747158523863050715693290963295227443043557\
                               6689664895044524452316173185640309871121722383113\
                               62229893423380308135336276614282806444486645238749\
                               30358907296290491560440772390713810515859307960866\
                               70172427121883998797908792274921901699720888093776\
                               65727333001053367881220235421809751254540594752243\
                               52584907711670556013604839586446706324415722155397\
                               53697817977846174064955149290862569321978468622482\
                               83972241375657056057490261407972968652414535100474\
                               82166370484403199890008895243450658541227588666881\
                               16427171479924442928230863465674813919123162824586\
                               17866458359124566529476545682848912883142607690042\
                               24219022671055626321111109370544217506941658960408\
                               07198403850962455444362981230987879927244284909188\
                               84580156166097919133875499200524063689912560717606\
                               05886116467109405077541002256983155200055935729725\
                               71636269561882670428252483600823257530420752963450";

   int iter = 0;
   size_t window;
   char buffer[5];
   window = bignumber.copy(buffer,iter,iter+5);

   cout << window << endl;

   return 0;
}

This is for project Euler problem 8 if you care. Thanks for your he开发者_C百科lp.


I believe you misread the documentation of basic_string::copy. From this page :

size_type copy( Char* s, size_type count, size_type index = 0 ) const;

Copies count characters from the position, starting at index to the given character string s. The resulting string is not NULL terminated.

Your use of the first parameter is correct (buffer being a char array, it will decay to a char pointer upon function argument passing), but your second and third arguments aren't :

  • size_type count is the number of characters to copy : you are providing 0 while you seem to want 5
  • size_type index is the starting index for character copying : you are providing 5 while you apparently need 0 (copy count characters from the start of the string). The parameter happens to have a default argument value of 0 : you don't have to provide any value here.

In the end, you could do :

const size_t window = bignumber.copy(buffer, sizeof(buffer));

Notice that I've used sizeof(buffer) rather than the magic value 5 to avoid introducing a bug if the buffer size is changed without reflecting the modification to this call. Also be aware that buffer cannot be simply outputted to std::cout after the call to copy because it is not a null terminated string.


Now, if working with std::string only is an option (and in most cases, it should be), you might as well be using basic_string::substr :

basic_string substr( size_type index = 0, size_type count = npos ) const;

Returns a substring of the current string, starting at the given position index and having length of count characters.

For example :

const std::string substring = bignumber.substr(0, 5);

Contrary to the copy solution, there is no possible size issue here, and the result can be outputted to std::cout without any problem. In other words : it's much safer.


The substr member function would be the more conventional solution to this problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消