我有他的代码:
int setAttrib(const string& name, int components){
// here I don't even touch 'name'
if(components == 2) return 3;
else return 1;
}
Run Code Online (Sandbox Code Playgroud)
我用这种方式调用函数:
setAttrib("position", 3);
Run Code Online (Sandbox Code Playgroud)
我正在使用xcode profiler分析内存,并且在函数调用中,std :: string正在进行分配.
这是为什么?
编辑:
避免这种分配的最佳方法是什么?因为我调用该函数很多,并在约10秒的时间,我结束了对分配在该行10MB.
谢谢.
我一直认为a std::string
是作为C char数组字符串的STL包装器实现的.但仔细观察设计,我注意到它没有任何提示或标志是一个被包裹的c字符串.对于我所知,我std::string
可以在内部做任何事情!
c_str()
当然有一种方法我认为它返回了内部char数组,但是如何知道该方法是否不会从它存储的任何数据中创建一个新的 c char数组并返回它?
说真的,如何std::string
实施?它(看起来像)只是C char数组的包装器,还是别的?还是两者的混合?甚至可以成为两个有条件的?
我有两个向量,一个是我想要擦除的另一个向量的索引向量.目前我正在做以下事情:
#include <vector>
#include <iostream>
#include <string>
int main() {
std::vector<std::string> my_vec;
my_vec.push_back("one");
my_vec.push_back("two");
my_vec.push_back("three");
my_vec.push_back("four");
my_vec.push_back("five");
my_vec.push_back("six");
std::vector<int> remove_these;
remove_these.push_back(0);
remove_these.push_back(3);
// remove the 1st and 4th elements
my_vec.erase(my_vec.begin() + remove_these[1]);
my_vec.erase(my_vec.begin() + remove_these[0]);
my_vec.erase(remove_these.begin(), remove_these.end());
for (std::vector<std::string>::iterator it = my_vec.begin(); it != my_vec.end(); ++it)
std::cout << *it << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
但我认为这是不优雅和低效的.此外,我认为我必须小心对remove_these
矢量进行排序并从最后开始(这就是我在索引0之前擦除索引3的原因).我想有一个擦除命令,类似于
my_vec.erase(remove_these.begin(), remove_these.end());
Run Code Online (Sandbox Code Playgroud)
但当然这不会起作用,因为my_vec.erase()
期望迭代器引用相同的向量.
该cppreference页说,有关std::basic_string::swap
它具有恒定的复杂性.我认为这意味着不能发生复制内容,只能交换指针或类似内容.我写了一个测试代码并且经历过它确实在VS2010下移动了内容.测试代码:
std::string s1("almafa");
std::string s2("kortefa");
std::cout << "s1.c_str(): "<< (void*)s1.c_str() << std::endl;
std::cout << "s2.c_str(): "<< (void*)s2.c_str() << std::endl;
std::cout << "SWAP!" << std::endl;
s1.swap(s2);
std::cout << "s1.c_str(): "<< (void*)s1.c_str() << std::endl;
std::cout << "s2.c_str(): "<< (void*)s2.c_str() << std::endl;
Run Code Online (Sandbox Code Playgroud)
g ++ 4.6.3上的输出
s1.c_str(): 0x22fe028
s2.c_str(): 0x22fe058
SWAP!
s1.c_str(): 0x22fe058
s2.c_str(): 0x22fe028
Run Code Online (Sandbox Code Playgroud)
VS2010的输出
s1.c_str(): 000000000022E2D0
s2.c_str(): 000000000022E320
SWAP!
s1.c_str(): 000000000022E2D0
s2.c_str(): 000000000022E320
Run Code Online (Sandbox Code Playgroud)
它是标准的分歧还是我不知道的事情发生了?
我在发送从指向结构的指针构建的 zmq 消息时遇到问题,该结构包含其他结构。
服务器代码:
#include <zmq.hpp>
#include <string>
#include <iostream>
using namespace zmq;
using namespace std;
struct structB{
int a;
string c;
};
struct structC{
int z;
struct structB b;
};
int main()
{
context_t context(1);
socket_t *socket = new socket_t(context,ZMQ_REP);
socket->bind("tcp://*:5555");
message_t *request = new message_t();
socket->recv(request);
struct structB messageB;
messageB.a=0;
messageB.c="aa";
struct structC *messageC = new struct structC;
messageC->z = 4;
messageC->b = messageB;
char *buffer = (char*)(messageC);
message_t *reply = new message_t((void*)buffer,
+sizeof(struct structB)
+sizeof(struct structC)
,0); …
Run Code Online (Sandbox Code Playgroud) 根据http://www.cplusplus.com/reference/string/string/上的文字,C++中的字符串库是一个类,而不仅仅是"内存数组中的单纯字符序列".我写了这段代码以了解更多信息:
string s = "abcd";
cout << &s << endl; // This gives an address
cout << s[0] << endl; // This gives 'a'
cout << &s[0] << endl; // This gives "abcd"
Run Code Online (Sandbox Code Playgroud)
我有一些问题:1.C++中的字符串库是否仍然是一个序列字符数组?2.如何获取字符串中每个字符的地址?(在代码中,我可以检索每个字符,但无法获取其地址& operator
)