【发布时间】:2021-01-16 03:28:23
【问题描述】:
std::string api_key_ = "123456789-5c51509f-8c5c5dc2-b6557";
std::pair<std::string_view, std::string_view> yy = std::make_pair("AccessKeyId", api_key_);
std::cout << "yy.second:" << yy.second << std::endl;
用c++17编译会输出:
yy.second:9-5c51509f-8c5c5dc2-b6557
虽然这很好,但没有扣除
std::string api_key_ = "123456789-5c51509f-8c5c5dc2-b6557";
std::string_view sv = api_key_;
std::cout << "sv:" << sv << std::endl;
std::pair<std::string_view, std::string_view> xx = std::make_pair<std::string_view, std::string_view>("AccessKeyId", api_key_);
std::cout << "xx.second:" << xx.second << std::endl;
输出:
sv:123456789-5c51509f-8c5c5dc2-b6557
xx.second:123456789-5c51509f-8c5c5dc2-b6557
谁能解释一下?谢谢。 完整代码如下:
#include <unordered_map>
#include <iostream>
#include <variant>
#include <deque>
#include <vector>
int main() {
std::string api_key_ = "123456789-5c51509f-8c5c5dc2-b6557";
std::string_view sv = api_key_;
std::cout << "sv:" << sv << std::endl;
std::pair<std::string_view, std::string_view> xx = std::make_pair<std::string_view, std::string_view>("AccessKeyId", api_key_);
std::cout << "xx.second:" << xx.second << std::endl;
std::pair<std::string_view, std::string_view> yy = std::make_pair("AccessKeyId", api_key_);
std::cout << "yy.second:" << yy.second << std::endl;
}
编译输出:
$ g++ ./try2.cpp -std=c++17
$./a.out
sv:123456789-5c51509f-8c5c5dc2-b6557
xx.second:123456789-5c51509f-8c5c5dc2-b6557
yy.second:9-5c51509f-8c5c5dc2-b6557
【问题讨论】: