string和char*的区别以及const_cast<>()

const_cast<char*>(Text.c_str())

#include<iostream>
#include<string>
#include<vector>
using namespace std;
 
void some_func( char * s)
{
     s[0] = 'X' ;
     cout<<s<<endl;
}
 
int main()
{
     string myStr = "hello" ;
     
     vector< char > str(myStr.begin(), myStr.end());
     str.push_back( '\0' );
 
     some_func(&str[0]);
 
     return 0;
}


Does the unpleasant C library function alter the data? If not, then simply
cast away the constness.

If it does, then you have to consider:

(1) Is it okay to alter the data at the address specified by c_str?

If so,

(1.a) Just cast away the constness and let it be altered.

If not,

(1.b) You'll have to make a copy.



The std::string manages it's own memory internally which is why, when it returns a pointer to that memory directly as it does with the c_str() function it makes sure it's constant so that your compiler will warn you if you try to do something incredibly silly like attempt to change it.


Using const_cast in that way literally casts away such safety and is only an arguably acceptable practice if you are  absolutely  sure that memory will not be modified. If you can't guarantee this then you must copy the string and use the copy; it's certainly a lot safer to do this in any event.

Here's a variation of 7stud's safe approach:.
Code:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>
#include <vector>
 
namespace
{
   char * GetNonConstStr( const std::string& s)
   {
   //return non-constant copy of s.c_str()
   static std::vector< char > var;
   var.assign(s.begin(),s.end());
   var.push_back( '\0' );
   return &var[0];
   }
   
   void someCFunction( char * str)
   {
     std::cout<<str<<std::endl;
   }
}
 
int main()
{
std::string s( "hello world" );
std::string t( "hello multiverse" );
 
someCFunction(GetNonConstStr(s));
someCFunction(GetNonConstStr(t));
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
函数名, vector<any> args) { auto func = funcMap.find(funcName); if (func != funcMap.end()) { return any_cast<int>((func->second)(args)); } return 0; } 针对这个问题,可以进行以下优化: 1.修改map表:可以使用unordered_map代替map,因为unordered_map使用哈希表,可以快速查找元素,而map使用红黑树,查找元素比较慢。 2.修改函数定义:可以将函数返回值类型设置为auto,让编译器自动推导返回值类型。 3.修改funAll函数:可以使用auto关键字来自动推导func的类型,以及使用const引用参数来避免不必要的复制。同时可以将if语句的find()和end()合并为一个语句,提高代码的可读性和效率。 优化后的代码如下: const string FUNC1 = "func1"; const string FUNC2 = "func2"; const string FUNC3 = "func3"; auto func1(int a, int b) { return a + b; } auto func2(int a, float b, float c) { return float(a) + b + c; } auto func3(int a, const char* b) { int result = a; auto length = strlen(b); for (size_t i = 0; i < length; i++) { result += b[i] - '0'; } return result; } unordered_map<string, function<any(vector<any>&)>> funcMap = { {FUNC1, [](vector<any>& args) { return func1(any_cast<int>(args[0]), any_cast<int>(args[1])); }}, {FUNC2, [](vector<any>& args) { return func2(any_cast<int>(args[0]), any_cast<float>(args[1]), any_cast<float>(args[2])); }}, {FUNC3, [](vector<any>& args) { return func3(any_cast<int>(args[0]), any_cast<const char*>(args[1])); }} }; int funAll(const string& funcName, vector<any>& args) { auto func = funcMap.find(funcName); if (func != funcMap.end()) { return any_cast<int>((func->second)(args)); } return 0; }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值