#include <iostream>
#include <string>
class MyClass
{
public:
virtual void fun1() = 0;
private:
};
class A : public MyClass {
public:
A() {}
~A() {}
};
void fun(const std::string& str) {
std::cout << "std::move不会转移str参数" << str;
}
void fun2(std::string str) {
std::cout << "std::move会转移str参数" << str;
}
int main()
{
//A继承纯虚类MyClass 继承不用是可以编译过的
//声明变量A a; 编译报错
std::string test1("11");
fun(std::move(test1));
//test1="11"不为空
std::string test2("11");
fun2(std::move(test2));
//test2 = "" 为空
std::move(test1); //不为空 ,不转移所有权 warning C4834: 放弃具有 "nodiscard" 属性的函数的返回值
//test1 = "11"
std::string mm = std::move(test1);//转移所有权
//test1 = ""
//lambda表达式
std::string mm1;
auto ld = [&]() ->void {
mm1 = "11"; //ok
};
auto ld02 = []() ->void {
//mm1 = "11"; // erro 报错 error C3493: 无法隐式捕获“mm1”,因为尚未指定默认捕获模式
};
}
============================================================================
//char data[16] = { "hello world 123" };
// 123 world hello
#include <iostream>
#include <string>
#include <sstream>
#include <algorithm>
std::string reverseWords(const std::string& str) {
std::istringstream iss(str);
std::string word;
std::string reversedStr;
while (iss >> word) {
reversedStr = word + " " + reversedStr;
}
// 移除末尾的空格
reversedStr.erase(reversedStr.size() - 1);
return reversedStr;
}
int main() {
char a[] = "hello world 123";
std::string str(a);
std::string reversedStr = reverseWords(str);
// 123 world hello
// 将字符串按空格分割成单词,然后反转整个字符串
std::reverse(reversedStr.begin(), reversedStr.end());
// 输出结果
std::cout << reversedStr << std::endl;
return 0;
}
c++面试八股一
于 2024-04-29 22:02:21 首次发布