string::assign

本文介绍了C++中使用basic_string的assign方法来从string类型变量或const char*中读取部分字符串到string变量中的方法。包括不同类型的参数设置及其实现的功能,如指定字符串的起始位置和长度等。

从string类型变量或const char * 中读取部分字符串到string变量中:
赋值(assign)
语法:

basic_string &assign( const basic_string &str );
basic_string &assign( const char *str );
basic_string &assign( const char *str, size_type num );
basic_string &assign( const basic_string &str, size_type index, size_type len );
basic_string &assign( size_type num, char ch );
函数以下列方式赋值:

用str为字符串赋值,
用str的开始num个字符为字符串赋值,
用str的子串为字符串赋值,子串以index索引开始,长度为len
用num个字符ch为字符串赋值.

#include<iostream>
#include<string>
using namespace std;
int main(){
string s = "hello,world";
string m;
m.assign(s,5,3);
cout<<m<<endl;   //输出为 ,wo
const char*num=""qazwsx,ed;
string n;
n.assign(&num[0],3);
cout<<num<<endl; //输出为qazwsx,ed
cout<<n<<endl;  //输出位qaz
return 0;
}
请仔细阅读和分析下面函数,进行优化后,采用C/C++11标准,完整推导并重构可编译的全部代码 特别注意: 1.保持所有原始功能不变 2.提高执行效率,降低计算复杂度 3.已经给定的结构体名字和元素不要更改,详细的中文注释 4.自动添加中文注释说明功能逻辑 5.不使用 auto,使用显式 for 循环 6.结构体采用32位定义 7.不要使用小函数,保持原始的函数定义 8.严格保持protobuf字段映射关系 函数中的 HDDMXng::Arc::Arc 映射为 message Arc { optional uint32 flags = 1; optional uint32 sitetype = 2; optional uint32 element = 3; optional uint32 indexinelement = 4; optional uint32 frompin = 5; optional uint32 topin = 6; optional uint32 graphid = 7; optional uint32 speedidx = 8; optional uint32 wire0 = 9; optional uint32 wire1 = 10; } 将 _BYTE arc_msg[8] 映射为 HDDMXng::Arc arc_msg; void __fastcall HDDMArc::getName(HDDMArc *this) { HDDMArc *HDDMArc; // rsi MAPDST bool v4; // zf __int64 Element; // rax __int64 v6; // rdi HDDMElementPin *FromElementPinEv; // r12 HDDMElementPin *ToElementPinEv; // r14 const std::string *Name; // rax __int64 v10; // rdi const std::string *NameEv; // rax __int64 v12; // rax __int64 v13; // rdi int v14; // eax __int64 v15; // rdi __int64 v16; // rcx __int64 v17; // rsi __int64 v18; // rax unsigned __int16 v19; // dx __int64 v20; // rcx __int64 v21; // r14 __int64 v22; // rdi char v23; // al char *v24; // rdx __int64 *v25; // rax __int64 v26; // rdi _QWORD *v27; // rax _QWORD *v28; // rax _QWORD *v29; // rax __int64 v30; // rdi __int64 v31; // rdi __int64 v32; // rdi __int64 v33; // rdi __int64 v34; // rdi __int64 v35; // rdi __int64 v36; // rdi __int64 v37; // rdi __int64 v38; // rdi int v39; // eax int v40; // eax int v41; // eax int v42; // eax int v43; // eax int v44; // eax int v45; // eax int v46; // eax int v47; // eax int v48; // eax int v49; // eax int v50; // eax int v51; // eax int v52; // eax int v53; // edx int v54; // edx int v55; // edx int v56; // edx int v57; // edx int v58; // edx int v59; // edx int v60; // edx int v61; // edx int v62; // edx int v63; // edx int v64; // edx int v65; // edx int v66; // edx int v67; // edx char v68; // [rsp+27h] [rbp-121h] BYREF char v69; // [rsp+28h] [rbp-120h] BYREF char v70; // [rsp+29h] [rbp-11Fh] BYREF char v71; // [rsp+2Ah] [rbp-11Eh] BYREF char v72; // [rsp+2Bh] [rbp-11Dh] BYREF _BYTE v73[4]; // [rsp+2Ch] [rbp-11Ch] BYREF _QWORD v74[2]; // [rsp+30h] [rbp-118h] BYREF _QWORD v75[2]; // [rsp+40h] [rbp-108h] BYREF _QWORD v76[2]; // [rsp+50h] [rbp-F8h] BYREF _QWORD v77[2]; // [rsp+60h] [rbp-E8h] BYREF _QWORD v78[2]; // [rsp+70h] [rbp-D8h] BYREF _QWORD v79[2]; // [rsp+80h] [rbp-C8h] BYREF _QWORD v80[2]; // [rsp+90h] [rbp-B8h] BYREF _QWORD v81[2]; // [rsp+A0h] [rbp-A8h] BYREF _QWORD v82[2]; // [rsp+B0h] [rbp-98h] BYREF _QWORD v83[2]; // [rsp+C0h] [rbp-88h] BYREF __int64 v84; // [rsp+D0h] [rbp-78h] BYREF _QWORD v85[2]; // [rsp+E0h] [rbp-68h] BYREF _QWORD v86[2]; // [rsp+F0h] [rbp-58h] BYREF _QWORD v87[9]; // [rsp+100h] [rbp-48h] BYREF v4 = (HDDMArc->arc_code & 0x400) == 0; v74[0] = (char *)&std::string::_Rep::_S_empty_rep_storage + 24; v75[0] = (char *)&std::string::_Rep::_S_empty_rep_storage + 24; v76[0] = (char *)&std::string::_Rep::_S_empty_rep_storage + 24; if ( v4 ) { Element = HDDMArc::getElement(HDDMArc); if ( Element ) std::string::string((std::string *)v80, (const std::string *)(Element + 8)); else std::string::string(v80, "noElement", &v71); std::string::assign((std::string *)v74, (const std::string *)v80); v6 = v80[0] - 24LL; if ( (_UNKNOWN *)(v80[0] - 24LL) != &std::string::_Rep::_S_empty_rep_storage ) { if ( &_pthread_key_create ) { v52 = _InterlockedExchangeAdd((volatile signed __int32 *)(v80[0] - 8LL), 0xFFFFFFFF); } else { v53 = *(_DWORD *)(v80[0] - 8LL); *(_DWORD *)(v80[0] - 8LL) = v53 - 1; v52 = v53; } if ( v52 <= 0 ) std::string::_Rep::_M_destroy(v6, v87); } FromElementPinEv = (HDDMElementPin *)HDDMArc::getFromElementPin(HDDMArc); ToElementPinEv = (HDDMElementPin *)HDDMArc::getToElementPin(HDDMArc); if ( FromElementPinEv ) { Name = (const std::string *)HDDMElementPin::getName(FromElementPinEv); std::string::string((std::string *)v81, Name); } else { std::string::string(v81, "noPin0", &v72); } std::string::assign((std::string *)v75, (const std::string *)v81); v10 = v81[0] - 24LL; if ( (_UNKNOWN *)(v81[0] - 24LL) != &std::string::_Rep::_S_empty_rep_storage ) { if ( &_pthread_key_create ) { v51 = _InterlockedExchangeAdd((volatile signed __int32 *)(v81[0] - 8LL), 0xFFFFFFFF); } else { v67 = *(_DWORD *)(v81[0] - 8LL); *(_DWORD *)(v81[0] - 8LL) = v67 - 1; v51 = v67; } if ( v51 <= 0 ) std::string::_Rep::_M_destroy(v10, v87); } if ( ToElementPinEv ) { NameEv = (const std::string *)HDDMElementPin::getName(ToElementPinEv); std::string::string((std::string *)v86, NameEv); } else { std::string::string(v86, "noPin1", v73); } std::string::assign((std::string *)v76, (const std::string *)v86); v12 = v86[0]; v13 = v86[0] - 24LL; if ( (_UNKNOWN *)(v86[0] - 24LL) != &std::string::_Rep::_S_empty_rep_storage ) { LABEL_11: if ( &_pthread_key_create ) { v14 = _InterlockedExchangeAdd((volatile signed __int32 *)(v13 + 16), 0xFFFFFFFF); } else { v54 = *(_DWORD *)(v12 - 8); *(_DWORD *)(v12 - 8) = v54 - 1; v14 = v54; } if ( v14 <= 0 ) std::string::_Rep::_M_destroy(v13, v87); } } else { if ( *(_QWORD *)(*(_QWORD *)(HDDMDeviceCache::m_devcache[(HIBYTE(HDDMArc->arc_code5) >> 4) & 0xF] + 168LL) + 8LL * (HIWORD(HDDMArc->arc_code) >> 6)) ) HDDMTileType::getName((HDDMTileType *)v77); else std::string::string(v77, "noTileType", &v68); std::string::swap((std::string *)v74, (std::string *)v77); v15 = v77[0] - 24LL; if ( (_UNKNOWN *)(v77[0] - 24LL) != &std::string::_Rep::_S_empty_rep_storage ) { if ( &_pthread_key_create ) { v49 = _InterlockedExchangeAdd((volatile signed __int32 *)(v77[0] - 8LL), 0xFFFFFFFF); } else { v65 = *(_DWORD *)(v77[0] - 8LL); *(_DWORD *)(v77[0] - 8LL) = v65 - 1; v49 = v65; } if ( v49 <= 0 ) std::string::_Rep::_M_destroy(v15, v87); } v16 = LOWORD(HDDMArc->arc_code2); v17 = 0; v18 = *(_QWORD *)(*(_QWORD *)(HDDMDeviceCache::m_devcache[(HIBYTE(HDDMArc->arc_code5) >> 4) & 0xF] + 168LL) + 8LL * (HIWORD(HDDMArc->arc_code) >> 6)); v19 = *(_WORD *)(v18 + 28); if ( (unsigned __int16)v16 < v19 ) v17 = *(_QWORD *)(v18 + 224) + 88 * v16; v20 = HIWORD(HDDMArc->arc_code2); v21 = 0; if ( v19 > (unsigned __int16)v20 ) v21 = *(_QWORD *)(v18 + 224) + 88 * v20; if ( v17 ) std::string::string((std::string *)v78, (const std::string *)(v17 + 32)); else std::string::string(v78, "noWire0", &v69); std::string::swap((std::string *)v75, (std::string *)v78); v22 = v78[0] - 24LL; if ( (_UNKNOWN *)(v78[0] - 24LL) != &std::string::_Rep::_S_empty_rep_storage ) { if ( &_pthread_key_create ) { v50 = _InterlockedExchangeAdd((volatile signed __int32 *)(v78[0] - 8LL), 0xFFFFFFFF); } else { v66 = *(_DWORD *)(v78[0] - 8LL); *(_DWORD *)(v78[0] - 8LL) = v66 - 1; v50 = v66; } if ( v50 <= 0 ) std::string::_Rep::_M_destroy(v22, v87); } if ( v21 ) std::string::string((std::string *)v79, (const std::string *)(v21 + 32)); else std::string::string(v79, "noWire1", &v70); std::string::swap((std::string *)v76, (std::string *)v79); v12 = v79[0]; v13 = v79[0] - 24LL; if ( (_UNKNOWN *)(v79[0] - 24LL) != &std::string::_Rep::_S_empty_rep_storage ) goto LABEL_11; } std::string::string(v82, ">", v87); v23 = BYTE1(HDDMArc->arc_code); if ( (v23 & 0x40) != 0 ) { std::string::append((std::string *)v82, ">", 1u); v23 = BYTE1(HDDMArc->arc_code); } v24 = (char *)&std::string::_Rep::_S_empty_rep_storage + 24; v83[0] = (char *)&std::string::_Rep::_S_empty_rep_storage + 24; if ( (v23 & 0x20) == 0 ) { std::string::assign((std::string *)v83, "<", 1u); if ( (HDDMArc->arc_code & 0x8000) != 0 ) std::string::append((std::string *)v83, "<", 1u); v24 = (char *)v83[0]; } v87[0] = (char *)&std::string::_Rep::_S_empty_rep_storage + 24; std::string::reserve((std::string *)v87, *((_QWORD *)v24 - 3) + 1LL); std::string::append((std::string *)v87, (const std::string *)v83); std::string::replace((std::string *)v87, *(_QWORD *)(v87[0] - 24LL), 0, "-", 1u); v25 = (__int64 *)std::string::append((std::string *)v87, (const std::string *)v82); v84 = *v25; *v25 = (__int64)&std::string::_Rep::_S_empty_rep_storage + 24; v26 = v87[0] - 24LL; if ( (_UNKNOWN *)(v87[0] - 24LL) != &std::string::_Rep::_S_empty_rep_storage ) { if ( &_pthread_key_create ) { v46 = _InterlockedExchangeAdd((volatile signed __int32 *)(v87[0] - 8LL), 0xFFFFFFFF); } else { v63 = *(_DWORD *)(v87[0] - 8LL); *(_DWORD *)(v87[0] - 8LL) = v63 - 1; v46 = v63; } if ( v46 <= 0 ) std::string::_Rep::_M_destroy(v26, v86); } v85[0] = (char *)&std::string::_Rep::_S_empty_rep_storage + 24; std::string::reserve((std::string *)v85, *(_QWORD *)(v74[0] - 24LL) + 1LL); std::string::append((std::string *)v85, (const std::string *)v74); std::string::replace((std::string *)v85, *(_QWORD *)(v85[0] - 24LL), 0, ".", 1u); v27 = (_QWORD *)std::string::append((std::string *)v85, (const std::string *)v75); v86[0] = *v27; *v27 = (char *)&std::string::_Rep::_S_empty_rep_storage + 24; v28 = (_QWORD *)std::string::append((std::string *)v86, (const std::string *)&v84); v87[0] = *v28; *v28 = (char *)&std::string::_Rep::_S_empty_rep_storage + 24; v29 = (_QWORD *)std::string::append((std::string *)v87, (const std::string *)v76); *(_QWORD *)&this->arc_code = *v29; *v29 = (char *)&std::string::_Rep::_S_empty_rep_storage + 24; v30 = v87[0] - 24LL; if ( (_UNKNOWN *)(v87[0] - 24LL) != &std::string::_Rep::_S_empty_rep_storage ) { if ( &_pthread_key_create ) { v44 = _InterlockedExchangeAdd((volatile signed __int32 *)(v87[0] - 8LL), 0xFFFFFFFF); } else { v61 = *(_DWORD *)(v87[0] - 8LL); *(_DWORD *)(v87[0] - 8LL) = v61 - 1; v44 = v61; } if ( v44 <= 0 ) std::string::_Rep::_M_destroy(v30, v81); } v31 = v86[0] - 24LL; if ( (_UNKNOWN *)(v86[0] - 24LL) != &std::string::_Rep::_S_empty_rep_storage ) { if ( &_pthread_key_create ) { v48 = _InterlockedExchangeAdd((volatile signed __int32 *)(v86[0] - 8LL), 0xFFFFFFFF); } else { v64 = *(_DWORD *)(v86[0] - 8LL); *(_DWORD *)(v86[0] - 8LL) = v64 - 1; v48 = v64; } if ( v48 <= 0 ) std::string::_Rep::_M_destroy(v31, v87); } v32 = v85[0] - 24LL; if ( (_UNKNOWN *)(v85[0] - 24LL) != &std::string::_Rep::_S_empty_rep_storage ) { if ( &_pthread_key_create ) { v42 = _InterlockedExchangeAdd((volatile signed __int32 *)(v85[0] - 8LL), 0xFFFFFFFF); } else { v62 = *(_DWORD *)(v85[0] - 8LL); *(_DWORD *)(v85[0] - 8LL) = v62 - 1; v42 = v62; } if ( v42 <= 0 ) std::string::_Rep::_M_destroy(v32, v87); } v33 = v84 - 24; if ( (_UNKNOWN *)(v84 - 24) != &std::string::_Rep::_S_empty_rep_storage ) { if ( &_pthread_key_create ) { v39 = _InterlockedExchangeAdd((volatile signed __int32 *)(v84 - 8), 0xFFFFFFFF); } else { v56 = *(_DWORD *)(v84 - 8); *(_DWORD *)(v84 - 8) = v56 - 1; v39 = v56; } if ( v39 <= 0 ) std::string::_Rep::_M_destroy(v33, v87); } v34 = v83[0] - 24LL; if ( (_UNKNOWN *)(v83[0] - 24LL) != &std::string::_Rep::_S_empty_rep_storage ) { if ( &_pthread_key_create ) { v41 = _InterlockedExchangeAdd((volatile signed __int32 *)(v83[0] - 8LL), 0xFFFFFFFF); } else { v55 = *(_DWORD *)(v83[0] - 8LL); *(_DWORD *)(v83[0] - 8LL) = v55 - 1; v41 = v55; } if ( v41 <= 0 ) std::string::_Rep::_M_destroy(v34, v87); } v35 = v82[0] - 24LL; if ( (_UNKNOWN *)(v82[0] - 24LL) != &std::string::_Rep::_S_empty_rep_storage ) { if ( &_pthread_key_create ) { v45 = _InterlockedExchangeAdd((volatile signed __int32 *)(v82[0] - 8LL), 0xFFFFFFFF); } else { v57 = *(_DWORD *)(v82[0] - 8LL); *(_DWORD *)(v82[0] - 8LL) = v57 - 1; v45 = v57; } if ( v45 <= 0 ) std::string::_Rep::_M_destroy(v35, v87); } v36 = v76[0] - 24LL; if ( (_UNKNOWN *)(v76[0] - 24LL) != &std::string::_Rep::_S_empty_rep_storage ) { if ( &_pthread_key_create ) { v43 = _InterlockedExchangeAdd((volatile signed __int32 *)(v76[0] - 8LL), 0xFFFFFFFF); } else { v58 = *(_DWORD *)(v76[0] - 8LL); *(_DWORD *)(v76[0] - 8LL) = v58 - 1; v43 = v58; } if ( v43 <= 0 ) std::string::_Rep::_M_destroy(v36, v87); } v37 = v75[0] - 24LL; if ( (_UNKNOWN *)(v75[0] - 24LL) != &std::string::_Rep::_S_empty_rep_storage ) { if ( &_pthread_key_create ) { v47 = _InterlockedExchangeAdd((volatile signed __int32 *)(v75[0] - 8LL), 0xFFFFFFFF); } else { v59 = *(_DWORD *)(v75[0] - 8LL); *(_DWORD *)(v75[0] - 8LL) = v59 - 1; v47 = v59; } if ( v47 <= 0 ) std::string::_Rep::_M_destroy(v37, v87); } v38 = v74[0] - 24LL; if ( (_UNKNOWN *)(v74[0] - 24LL) != &std::string::_Rep::_S_empty_rep_storage ) { if ( &_pthread_key_create ) { v40 = _InterlockedExchangeAdd((volatile signed __int32 *)(v74[0] - 8LL), 0xFFFFFFFF); } else { v60 = *(_DWORD *)(v74[0] - 8LL); *(_DWORD *)(v74[0] - 8LL) = v60 - 1; v40 = v60; } if ( v40 <= 0 ) std::string::_Rep::_M_destroy(v38, v87); } }
最新发布
10-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值