在C ++中处理字符串时如何使用memset? [英] How to use memset while handling strings in C++?

查看:173
本文介绍了在C ++中处理字符串时如何使用memset?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我来自Python背景,最近学习C ++.我正在学习一个名为memset的C/C ++函数,并从网站 https://www.geeksforgeeks.org/memset-in-cpp/出现一些编译错误:

I am from Python background and recently learning C++. I was learning a C/C++ function called memset and following the online example from website https://www.geeksforgeeks.org/memset-in-cpp/ where I got some compilation errors:

/**
 * @author      : Bhishan Poudel
 * @file        : a02_memset_geeks.cpp
 * @created     : Wednesday Jun 05, 2019 11:07:03 EDT
 * 
 * Ref: 
 */

#include <iostream>
#include <vector>
#include <cstring>

using namespace std;

int main(int argc, char *argv[]){
    char str[] = "geeksforgeeks";

    //memset(str, "t", sizeof(str));
    memset(str, 't', sizeof(str));

    cout << str << endl;

    return 0;
}

使用单引号't'时出错
这样会打印出多余的字符.

Error when using single quotes 't'
This prints extra characters.

tttttttttttttt!R@`

在双引号中使用"t"时出错

$ g++ -std=c++11 a02_memset_geeks.cpp 
a02_memset_geeks.cpp:17:5: error: no matching function for call to 'memset'
    memset(str, "t", sizeof(str));
    ^~~~~~
/usr/include/string.h:74:7: note: candidate function not viable: no known
      conversion from 'const char [2]' to 'int' for 2nd argument
void    *memset(void *, int, size_t);
         ^
1 error generated.

如何在C ++中使用内存集?

How to use the memset in C++ ?

进一步研究
这里给出了缺点memset的优秀教程: https://web.archive.org/web/20170702122030/https:/augias.org/paercebal/tech_doc/doc.en/cp.memset_is_evil.html

Further Study
Excellent tutorial with shortcomings of memset is given here: https://web.archive.org/web/20170702122030/https:/augias.org/paercebal/tech_doc/doc.en/cp.memset_is_evil.html

推荐答案

此声明

char str[] = "geeksforgeeks";

声明一个字符数组,其中包含一个字符串,该字符串是包含终止零符号'\0'的字符序列.

declares a character array that contains a string that is a sequence of characters including the terminating zero symbol '\0'.

您可以按照以下等效方式想象该声明

You can imagine the declaration the following equivalent way

char str[] = 
{ 
    'g', 'e', 'e', 'k', 's', 'f', 'o', 'r', 'g', 'e', 'e', 'k', 's', '\0'
};

此函数memset

memset(str, 't', sizeof(str));

覆盖数组的所有字符,包括结尾的零.

overrides all characters of the array including the terminating zero.

所以下一条语句

cout << str << endl;

导致不确定的行为,因为它会输出字符,直到遇到终止的零为止.

results in undefined behaviour because it outpuuts characters until the terminating zero is encountered.

您可以改写

#include <iostream>
#include <cstring>

int main()
{
    char str[] = "geeksforgeeks";

    std::memset( str, 't', sizeof( str ) - 1 );

    std::cout << str << '\n';
}

或采用以下方式

#include <iostream>
#include <cstring>

int main()
{
    char str[] = "geeksforgeeks";

    std::memset( str, 't', std::strlen( str ) );

    std::cout << str << '\n';
}

这将使终止零在数组中保持不变.

That is keeping the terminating zero unchanged in the array.

如果要覆盖数组的所有字符(包括结尾的零),则应替换此语句

If you want to override all characters of the array including the terminating zero, then you should substitute this statement

std::cout << str << '\n';

此声明

std::cout.write( str, sizeof( str ) ) << '\n';

如下面程序所示,因为该数组现在不包含字符串.

as it is shown in the program below because the array now does not contain a string.

#include <iostream>
#include <cstring>

int main()
{
    char str[] = "geeksforgeeks";

    std::memset( str, 't', sizeof( str ) );

    std::cout.write( str, sizeof( str ) ) << '\n';
}

至此通话

memset(str, "t", sizeof(str));

然后,第二个参数的类型(即类型const char *)与具有int类型的第二个函数参数的类型不对应.参见函数的声明

then the type of the second argument (that is the type const char *) does not correspond to the type of the second function parameter that has the type int. See the declaration of the function

void * memset ( void * ptr, int value, size_t num );

因此,编译器会发出错误消息.

Thus the compiler issues an error message.

除了字符数组(即使在C ++中也经常使用)之外,您还可以使用模拟字符串的标准类std::string(或std::basic_string).

Apart from character arrays (that are used very often even in C++) you can use also the standard class std::string (or std::basic_string) that simulates strings.

在这种情况下,不需要使用标准的C函数memset来用单个字符填充字符串.最简单的方法是以下

In this case there is no need to use the standard C function memset to fill a string with a single character. The simplest way to do this is the following

#include <iostream>
#include <string>

int main()
{
    std::string s( "geeksforgeeks" );

    s.assign( s.length(), 't' );

    std::cout << s << '\n';
}

另一种方法是使用标头<algorithm>中声明的标准算法std::fillstd::fill_n.例如

Another way is to use the standard algorithm std::fill or std::fill_n declared in the header <algorithm>. For example

#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>

int main()
{
    std::string s( "geeksforgeeks" );

    std::fill( std::begin( s ), std::end( s ), 't' );

    std::cout << s << '\n';
}

#include <iostream>
#include <string>
#include <iterator>
#include <algorithm>

int main()
{
    std::string s( "geeksforgeeks" );

    std::fill_n( std::begin( s ), s.length(), 't' );

    std::cout << s << '\n';
}

您甚至可以使用以下方法之一的类std::string的方法replace

You even can use the method replace of the class std::string one of the following ways

#include <iostream>
#include <string>

int main()
{
    std::string s( "geeksforgeeks" );

    s.replace( 0, s.length(), s.length(), 't' );

    std::cout << s << '\n';
}

#include <iostream>
#include <string>

int main()
{
    std::string s( "geeksforgeeks" );

    s.replace( std::begin( s ), std::end( s ), s.length(), 't' );

    std::cout << s << '\n';
}

这篇关于在C ++中处理字符串时如何使用memset?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆