草庐IT

str_shuffle

全部标签

c++ - 为什么要在函数中使用 c_str()

这个问题在这里已经有了答案:Whydon'tthestd::fstreamclassestakeastd::string?(10个答案)关闭8年前。我正在阅读《C++Primer》一书及其使用的文件输入输出章节:ifstreaminfile(ifile.c_str());打开名称在字符串ifile中的文件。我尝试了代码,即使没有c_str()也能完美运行。那么使用它有什么意义呢?当我尝试从命令行参数打开文件时,我应该使用c_str()吗?我的意思是正确的用法:ifstreamfin(argv[1])或ifstreamfin(argv[1].c_str())

c++ - "if (getline(fin, str)) {}"是否符合C++11标准?

我检查了C++11标准,发现了以下事实:std::getline(fin,str)返回一个basic_ios对象,其类有一个成员函数explicitoperatorbool()const;basic_ios类没有成员函数operatorvoid*()const;作为C++11之前的版本。所以,我认为if(getline(fin,str)){}不符合标准。应该写成if(bool(getline(fin,str)){}。(但是,VC++2012对此用法给出警告。即强制void*为bool)我说的对吗? 最佳答案 代码是一致的。当对象自动

c++ - 从函数返回 'c_str'

这是我在网上找到的一个小型图书馆:constchar*GetHandStateBrief(constPostFlopState*state){staticstd::ostringstreamout;//...restofthefunction...returnout.str().c_str()}在我的代码中我这样做:constchar*d=GetHandStateBrief(&post);std::cout现在,起初d包含垃圾。然后我意识到我从函数中获取的C字符串在函数返回时被销毁,因为std::ostringstream是在堆栈上分配的。所以我补充说:returnstrdup(out

c++ - 调用空字符串的 c_str

这个代码片段是正确的还是会导致未定义的行为?std::strings;assert(strlen(s.c_str())==0);如果不是undefinedbehavior,上面的断言会通过吗? 最佳答案 这是完美定义的,断言通过了。c_str()函数将始终返回有效的以零结尾的C字符串。通常会使用empty()来测试空字符串。 关于c++-调用空字符串的c_str,我们在StackOverflow上找到一个类似的问题: https://stackoverflow

c++ - 尝试转换 str 字符串以用于 std::getline 定界符

我正在尝试获取std::string值并将其用作std::getline()函数中的分隔符但无论我做什么,编译器都会给我错误。这就是我想要做的:std::stringstreamss(s);std::stringitem;std::stringdelim="&&=";intssize=delim.size();intnewssize=ssize+1;chardel[SSIZE];//alsoherewhenitrygiveserrorstrcpy(del,delim.c_str());char*delrr[1024]=delim.c_str();//giveserrorwhile(st

c++ - 将 int vector 转换为 str vector

我正在尝试转换vector到vector.使用std::transform我用了std::to_string转换int至string但我不断收到错误消息。这是我的代码#include#include#include#includeintmain(){std::vectorv_int;std::vectorv_str;for(inti=0;i但是我收到了这个错误nomatchingfunctionforcallto'transform'std::transform(v_int.begin(),v_int.end(),v_str.begin(),std::to_string);^~~~~~

python - BeautifulSoup: AttributeError: 'str' 对象没有属性 'copy' 。在 Ubuntu 上工作,在 Windows 上失败

我在使用BeautifulSoup时遇到一个奇怪的错误。这是我正在运行的代码片段:whileTrue:listing_soup=soupify(urlget(page_url))foriinlisting_soup.findAll('div','searchResultContent'):#dosomestuff...这是抛出的异常:Traceback(mostrecentcalllast):File"C:\path\to\script.py",line716,inforiinlisting_soup.findAll('div','searchResultContent'):File"

windows - python TypeError : must be encoded string without NULL bytes, 不是 str

尝试熟悉python的标准库,并在我的Windows机器上对其进行一些处理。使用python2.7我有以下小脚本,它用于在目录中查找并在从文件名中删除数字后重命名其中的所有文件。我收到一个类型错误,提示“必须是没有NULL字节的编码字符串,而不是str”它调用了第5行和第18行,在下面注明,其中我使用了os.path.exists。如有任何帮助,我们将不胜感激!importos,re,string,globpath=os.path.normpath('C:\Users\me\PhotoProjects\ProjectName\ProjectPhotos\Modified\0-PyTes

python - 类型错误 : unbound method sadd() must be called with StrictRedis instance as first argument (got str instance instead)

我在python应用程序中有以下内容:fromredisimportRedis,StrictRedis......r=line.split("")[0]StrictRedis.sadd('my_set',r)我在标题中遇到错误。我做错了什么? 最佳答案 StrictRedis(host='localhost',port=6379,db=0,password=None,socket_timeout=None,connection_pool=None,charset='utf-8',errors='strict',unix_socket

mysql - 函数 str_to_date 的日期时间值不正确

我看不出这有什么问题:mysql>selectstr_to_date('3/8/201012:31:00AM','%c/%e/%Y%k:%i:%s%p');+-------------------------------------------------------------+|str_to_date('3/8/201012:31:00AM','%c/%e/%Y%k:%i:%s%p')|+-------------------------------------------------------------+|NULL|+-----------------------------