std::pair<std::string, std::string> key = std::make_pair(col1, col2);
时间: 2023-11-24 11:05:53 浏览: 36
这段代码使用了std::pair模板类,用于将两个不同类型的值组合成一个值。在这个例子中,std::make_pair函数将col1和col2组合成一个std::pair<std::string, std::string>类型的值,并将其赋值给了key变量。这个变量可以通过key.first和key.second来访问其中的两个值。
```C++
std::string col1 = "hello";
std::string col2 = "world";
std::pair<std::string, std::string> key = std::make_pair(col1, col2);
std::cout << key.first << " " << key.second << std::endl; // 输出 hello world
```
相关问题
std::string <==> ::std::string
`std::string`和`::std::string`是完全相同的,它们都是C++标准库中的字符串类。`std`是C++标准库的命名空间,因此`std::string`表示在`std`命名空间中定义的字符串类。而`::std::string`中的`::`表示全局命名空间,因此它也表示相同的字符串类。两者可以互换使用,没有任何区别。
示例代码:
```c++
#include <iostream>
#include <string>
int main() {
std::string str1 = "hello";
::std::string str2 = "world";
std::string str3 = str1 + ", " + str2;
std::cout << str3 << std::endl; // 输出:hello, world
return 0;
}
```
没有与这些操作数匹配的 "=" 运算符C/C++(349) listener_str.cpp(12, 23): 操作数类型为: std::shared_ptr<rclcpp::Subscription<std_msgs::msg::String, std::allocator<void>, rclcpp::message_memory_strategy::MessageMemoryStrategy<std_msgs::msg::String, std::allocator<void>>>> = std::shared_ptr<rclcpp::Subscription<const std::shared_ptr<std_msgs::msg::String_<std::allocator<void>>> &, std::allocator<void>, rclcpp::message_memory_strategy::MessageMemoryStrategy<const std::shared_ptr<std_msgs::msg::String_<std::allocator<void>>> &, std::allocator<void>>>>
这个错误通常是因为尝试将一个类型为`std::shared_ptr<rclcpp::Subscription<const std::shared_ptr<std_msgs::msg::String_<std::allocator<void>>> &, std::allocator<void>, rclcpp::message_memory_strategy::MessageMemoryStrategy<const std::shared_ptr<std_msgs::msg::String_<std::allocator<void>>> &, std::allocator<void>>>>`的变量赋值给一个类型为`std::shared_ptr<rclcpp::Subscription<std_msgs::msg::String, std::allocator<void>, rclcpp::message_memory_strategy::MessageMemoryStrategy<std_msgs::msg::String, std::allocator<void>>>>`的变量,这两个类型虽然都是指向`rclcpp::Subscription`的`shared_ptr`,但是模板参数不同,无法直接赋值。
解决方法是将两个类型匹配,可以通过使用`std::static_pointer_cast`将其中一个类型转换成另一个类型,例如:
```
std::shared_ptr<rclcpp::Subscription<std_msgs::msg::String, std::allocator<void>, rclcpp::message_memory_strategy::MessageMemoryStrategy<std_msgs::msg::String, std::allocator<void>>>> sub;
std::shared_ptr<rclcpp::Subscription<const std::shared_ptr<std_msgs::msg::String_<std::allocator<void>>> &, std::allocator<void>, rclcpp::message_memory_strategy::MessageMemoryStrategy<const std::shared_ptr<std_msgs::msg::String_<std::allocator<void>>> &, std::allocator<void>>>> sub_const;
// 将 sub_const 转换成 sub 的类型
sub = std::static_pointer_cast<rclcpp::Subscription<std_msgs::msg::String, std::allocator<void>, rclcpp::message_memory_strategy::MessageMemoryStrategy<std_msgs::msg::String, std::allocator<void>>>>(sub_const);
```