C++ 如何避免构造新字符串?
我有一个模拟2D向量的一维字符向量(这是一个要求)。这个向量是000111,它相当于2D的向量[0]=000和向量[1]=111。所以它有两个字符串,长度都相同(本例中为3)。我想将每个字符串设置为C++ 如何避免构造新字符串?,c++,string,vector,data-structures,hashtable,C++,String,Vector,Data Structures,Hashtable,我有一个模拟2D向量的一维字符向量(这是一个要求)。这个向量是000111,它相当于2D的向量[0]=000和向量[1]=111。所以它有两个字符串,长度都相同(本例中为3)。我想将每个字符串设置为std::unordered_map中的一个键,因此我正在执行以下操作: #include <iostream> #include <unordered_map> #include <string> #include <vector> int main
std::unordered_map
中的一个键,因此我正在执行以下操作:
#include <iostream>
#include <unordered_map>
#include <string>
#include <vector>
int main ()
{
std::unordered_map<std::string, int> mymap;
std::vector<char> keys(2 * 3); // 2 keys, each one of length 3
for(int i = 0; i < 2; ++i)
{
for(int j = 0; j < 3; ++j)
{
keys[j + i * 3] = (i) ? '1' : '0';
}
}
// keys = 000111
for(int i = 0; i < 2; ++i)
{
mymap[std::string(keys.begin() + i * 3, keys.begin() + (i + 1) * 3)] = i;
}
for (auto& x: mymap) {
std::cout << x.first << ": " << x.second << std::endl;
}
/*
Output:
111: 1
000: 0
*/
return 0;
}
#包括
#包括
#包括
#包括
int main()
{
std::无序图mymap;
向量键(2*3);//2个键,每个键的长度为3
对于(int i=0;i<2;++i)
{
对于(int j=0;j<3;++j)
{
键[j+i*3]=(i)?“1”:“0”;
}
}
//键=000111
对于(int i=0;i<2;++i)
{
mymap[std::string(key.begin()+i*3,key.begin()+(i+1)*3)]=i;
}
用于(自动&x:mymap){
std::cout我认为这是c++17字符串视图的替代品。字符串视图不拥有任何字符串,因此常量可能是一个问题(请参阅插入地图时的常量转换)
唯一需要做的改变是
const cast,你必须解决这个问题
多重映射的类型
注意#endif处的using语句
我只是将一个类、一个散列结构(std::!)和一些重载附加到代码中
#include <iostream>
#include <unordered_map>
#include <string>
#include <vector>
#ifdef HAS_STRING_VIEW
#include <string_view>
#else
class lps_noz_view{
public:
lps_noz_view() = delete;
lps_noz_view(const char* start, size_t size):start(start), stop(start + size){}
lps_noz_view(const lps_noz_view& ) = default;
lps_noz_view(lps_noz_view&& ) = default;
const char* begin(){ return start;}
const char* end(){ return stop;}
const char* begin() const{ return start;}
const char* end() const{ return stop;}
std::string to_string() const{ return std::string(start, stop);}
private:
const char* start;
const char* stop;
};
bool operator < (const lps_noz_view& lhs, const lps_noz_view& rhs){
return lhs.to_string() < rhs.to_string();
// or use strncmp to avoid creating strings =)
}
bool operator == (const lps_noz_view& lhs, const lps_noz_view& rhs){
return lhs.to_string() == rhs.to_string();
// strncmp
}
std::ostream& operator << (std::ostream& os, const lps_noz_view& rhs){
return os << rhs.to_string();
}
namespace std{
template<>
struct hash<lps_noz_view>
{
using argument_type = lps_noz_view;
using result_type = size_t;
size_t operator()(const lps_noz_view& arg) const{
return std::hash<std::string>()(std::string(arg.begin(), arg.end()));
}
};
};
using string_view = lps_noz_view;
#endif
//
int main ()
{
std::unordered_map<string_view, int> mymap;
std::vector<char> keys(2 * 3); // 2 keys, each one of length 3
for(int i = 0; i < 2; ++i)
{
for(int j = 0; j < 3; ++j)
{
keys[j + i * 3] = (i) ? '1' : '0';
}
}
// keys = 000111
for(int i = 0; i < 2; ++i)
{
mymap[string_view(const_cast<const char*>(&(*keys.begin()) + i * 3),
(i + 1) * 3)] = i;
}
for (auto& x: mymap) {
std::cout << x.first << ": " << x.second << std::endl;
}
/*
Output:
111: 1
000: 0
*/
return 0;
}
#包括
#包括
#包括
#包括
#ifdef具有字符串视图
#包括
#否则
类lps_noz_视图{
公众:
lps_noz_view()=删除;
lps_noz_视图(const char*start,size_t size):开始(start),停止(start+size){}
lps_noz_视图(常量lps_noz_视图&)=默认值;
lps_noz_视图(lps_noz_视图&&)=默认值;
const char*begin(){return start;}
const char*end(){return stop;}
const char*begin()const{return start;}
const char*end()const{return stop;}
std::string to_string()const{return std::string(start,stop);}
私人:
常量字符*开始;
常量字符*停止;
};
bool操作员<(常量lps\U noz\U视图和左侧、常量lps\U noz\U视图和右侧){
返回lhs.to_string() //或者使用strncmp避免创建字符串=)
}
布尔运算符==(常量lps\U noz\U视图和左视图,常量lps\U noz\U视图和右视图){
返回lhs.to_string()==rhs.to_string();
//strncmp
}
std::ostream&operator“构造很差?”是的。char(0)!=“0”
billy of me@BaummitAugen,你是对的,我更新了问题,请重新阅读!:)@gsamaras与以前一样的问题。;)试试键[j+I*3]=(I)?“1”:“0”;
你能使用
吗?你也可以滚动你自己的字符串视图。这让我不高兴,因为我必须构造一个新字符串。不要让这样愚蠢的事情影响你的快乐。如果你不高兴,我可以理解,因为你的程序的性能比预期的差10%,但不是因为你无法避免一个操作,它对性能的影响是未知的,可能可以忽略不计。“或者使用strncmp来避免创建字符串=)”,Giraffe你就是那个人..我是说动物!很好。我觉得这是一个合适的注释=)