专栏文章

提高组知识点查漏补缺

个人记录参与者 1已保存评论 0

文章操作

快速查看文章及其快照的属性,并进行相关操作。

当前评论
0 条
当前快照
1 份
快照标识符
@mio6cvcp
此快照首次捕获于
2025/12/02 14:05
3 个月前
此快照最后确认于
2025/12/02 14:05
3 个月前
查看原文

Linux 终端常用文件与目录操作命令

掌握这些命令是熟练使用 Linux 的基础。

目录操作

命令全称/含义功能常用示例与说明
pwdPrint Working Directory显示当前所在目录的绝对路径pwd
lsList列出目录中的内容ls (列出当前目录)ls /home (列出指定目录)ls -l (以长格式列出,显示详细信息)ls -a (列出所有文件,包括隐藏文件)ls -lh (以易读格式显示文件大小)
cdChange Directory切换当前工作目录cd /tmp (切换到指定目录)cd .. (切换到上一级目录)cdcd ~ (切换回家目录)cd - (切换到上一个所在目录)
mkdirMake directory创建新目录mkdir new_folder (创建单个目录)mkdir -p parent/child (递归创建多级目录)
rmdirRemove directory删除目录rmdir empty_dir (只能删除空目录)

文件操作

命令全称/含义功能常用示例与说明
touch-1. 创建新的空文件2. 更新文件时间戳touch file.txt (创建文件或更新时间戳)
cpCopy复制文件或目录cp file1.txt file2.txt (复制文件)cp -r dir1/ dir2/ (递归复制整个目录)
mvMove1. 移动文件或目录2. 重命名文件或目录mv old.txt new.txt (重命名)mv file.txt /tmp/ (移动到指定目录)
rmRemove删除文件或目录rm file.txt (删除文件)rm -r folder/ (递归删除目录)rm -f file.log (强制删除)警告:谨慎使用 rm -rf
catCatenate查看较短文件内容cat file.txt
less / more-分页查看较长文件内容less long_file.log(空格键翻页,q 退出,/word 搜索)
head-显示文件开头部分head -n 20 file.log (显示前20行)
tail-显示文件末尾部分tail -n 15 file.log (显示末尾15行)tail -f app.log (实时追踪日志)

文件内容处理与查找

命令全称/含义功能常用示例与说明
grepGlobal Regular Expression文本搜索工具grep "error" system.log (搜索字符串)grep -r "function" ./src/ (递归搜索)grep -i "warning" (忽略大小写搜索)
find-查找文件find /home -name "*.txt" (按名称查找)find . -type f -size +10M (查找大于10MB的文件)
wcWord Count统计文件信息wc file.txt (行数、单词数、字节数)wc -l access.log (只统计行数)

权限与所有权

命令全称/含义功能常用示例与说明
chmodChange Mode改变文件权限chmod +x script.sh (添加执行权限)chmod 755 file (数字模式设置权限)
chownChange Owner改变文件所有者chown user:group file.txt (改变所有者和组)chown -R user:group /project/ (递归改变)

其他实用命令

命令功能常用示例与说明
echo输出文本echo "Hello World"echo $PATH (输出环境变量)
file查看文件类型file archive.tar.gz (显示文件类型)
**`` (管道)**命令输出重定向
>>>输出重定向到文件ls > list.txt (覆盖写入)echo "new line" >> list.txt (追加写入)

使用技巧与注意事项

  1. 路径补全 (Tab Completion):按 Tab 键自动补全路径,按两次 Tab 列出所有选项
  2. 上下箭头:快速调用历史命令
  3. 通配符
    • * 代表任意多个字符(如 *.txt
    • ? 代表任意一个字符(如 file?.log
    • [abc] 匹配括号内任意一个字符(如 file[123].txt
  4. 超级用户权限:需要时在命令前加 sudo
  5. 谨慎使用 rm:Linux 删除文件不会进入回收站,直接永久删除
CPP
#include <iostream>
#include <vector>
#include <list>
#include <deque>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <string>
#include <stack>
#include <queue>

using namespace std;

// 打印容器内容的通用函数(使用迭代器)
template<typename Container>
void printContainer(const string& name, const Container& c) {
    cout << name << ": ";
    for (auto it = c.begin(); it != c.end(); ++it) {
        cout << *it << " ";
    }
    cout << endl;
}

// 针对map的打印特化版本
template<typename Key, typename Value>
void printContainer(const string& name, const map<Key, Value>& m) {
    cout << name << ": ";
    for (auto it = m.begin(); it != m.end(); ++it) {
        cout << "(" << it->first << ":" << it->second << ") ";
    }
    cout << endl;
}

// 针对unordered_map的打印特化版本
template<typename Key, typename Value>
void printContainer(const string& name, const unordered_map<Key, Value>& m) {
    cout << name << ": ";
    for (auto it = m.begin(); it != m.end(); ++it) {
        cout << "(" << it->first << ":" << it->second << ") ";
    }
    cout << endl;
}

int main() {
    cout << "=== STL 容器与迭代器示例 ===" << endl << endl;
    
    // 1. 序列容器示例
    cout << "1. 序列容器" << endl;
    
    // vector - 动态数组
    vector<int> vec = {5, 2, 8, 1, 9};
    vec.push_back(7); // 在末尾添加元素
    printContainer("vector", vec);
    cout << "vector 大小: " << vec.size() << ", 容量: " << vec.capacity() << endl;
    
    // list - 双向链表
    list<int> lst = {5, 2, 8, 1, 9};
    lst.push_front(0); // 在开头添加元素
    lst.sort(); // 链表有自己的sort方法
    printContainer("list", lst);
    
    // deque - 双端队列
    deque<int> dq = {5, 2, 8, 1, 9};
    dq.push_front(0);
    dq.push_back(7);
    printContainer("deque", dq);
    
    cout << endl;
    
    // 2. 关联容器示例
    cout << "2. 关联容器" << endl;
    
    // set - 有序唯一集合
    set<int> s = {5, 2, 8, 1, 9, 2, 5}; // 重复元素会被忽略
    s.insert(3);
    printContainer("set", s);
    
    // map - 键值对映射
    map<string, int> m = {
        {"Alice", 25},
        {"Bob", 30},
        {"Charlie", 35}
    };
    m["David"] = 28; // 添加新元素
    printContainer("map", m);
    
    // multiset - 允许重复键的集合
    multiset<int> ms = {5, 2, 8, 1, 9, 2, 5};
    printContainer("multiset", ms);
    
    cout << endl;
    
    // 3. 无序关联容器示例 (C++11)
    cout << "3. 无序关联容器 (哈希表)" << endl;
    
    // unordered_set - 哈希集合
    unordered_set<int> us = {5, 2, 8, 1, 9, 2, 5};
    printContainer("unordered_set", us);
    
    // unordered_map - 哈希映射
    unordered_map<string, int> um = {
        {"Alice", 25},
        {"Bob", 30},
        {"Charlie", 35}
    };
    printContainer("unordered_map", um);
    
    cout << endl;
    
    // 4. 容器适配器示例
    cout << "4. 容器适配器" << endl;
    
    // stack - 后进先出
    stack<int> st;
    for (int n : {1, 2, 3, 4, 5}) {
        st.push(n);
    }
    cout << "stack: ";
    while (!st.empty()) {
        cout << st.top() << " ";
        st.pop();
    }
    cout << endl;
    
    // queue - 先进先出
    queue<int> q;
    for (int n : {1, 2, 3, 4, 5}) {
        q.push(n);
    }
    cout << "queue: ";
    while (!q.empty()) {
        cout << q.front() << " ";
        q.pop();
    }
    cout << endl;
    
    // priority_queue - 优先级队列(默认最大堆)
    priority_queue<int> pq;
    for (int n : {5, 2, 8, 1, 9}) {
        pq.push(n);
    }
    cout << "priority_queue: ";
    while (!pq.empty()) {
        cout << pq.top() << " ";
        pq.pop();
    }
    cout << endl;
    
    cout << endl;
    
    // 5. 迭代器示例
    cout << "5. 迭代器使用示例" << endl;
    
    vector<int> numbers = {5, 2, 8, 1, 9, 3, 7, 4, 6};
    
    // 使用迭代器遍历
    cout << "使用迭代器遍历: ";
    for (auto it = numbers.begin(); it != numbers.end(); ++it) {
        cout << *it << " ";
    }
    cout << endl;
    
    // 使用反向迭代器
    cout << "使用反向迭代器: ";
    for (auto rit = numbers.rbegin(); rit != numbers.rend(); ++rit) {
        cout << *rit << " ";
    }
    cout << endl;
    
    // 使用迭代器和算法
    auto min_it = min_element(numbers.begin(), numbers.end());
    auto max_it = max_element(numbers.begin(), numbers.end());
    cout << "最小值: " << *min_it << ", 最大值: " << *max_it << endl;
    
    // 使用迭代器插入元素
    auto insert_it = numbers.begin();
    advance(insert_it, 3); // 将迭代器前进3个位置
    numbers.insert(insert_it, 99);
    cout << "在位置3插入99后: ";
    printContainer("numbers", numbers);
    
    // 使用迭代器删除元素
    auto remove_it = numbers.begin();
    advance(remove_it, 5); // 将迭代器前进5个位置
    numbers.erase(remove_it);
    cout << "删除位置5的元素后: ";
    printContainer("numbers", numbers);
    
    // 使用迭代器范围排序
    sort(numbers.begin(), numbers.end());
    cout << "排序后: ";
    printContainer("numbers", numbers);
    
    // 使用const迭代器(只读访问)
    cout << "使用const迭代器: ";
    for (auto cit = numbers.cbegin(); cit != numbers.cend(); ++cit) {
        // *cit = 10; // 错误:不能修改const迭代器指向的值
        cout << *cit << " ";
    }
    cout << endl;
    
    return 0;
}
欧拉图(Eulerian Graph) 欧拉图是图论中的一个重要概念,得名于瑞士数学家莱昂哈德·欧拉。欧拉图与著名的"七桥问题"密切相关,欧拉在解决这个问题时开创了图论的研究。
欧拉图的定义 欧拉路径(Eulerian Path) 通过图中每条边恰好一次的路径称为欧拉路径。
欧拉回路(Eulerian Circuit) 通过图中每条边恰好一次且起点和终点相同的路径称为欧拉回路。
欧拉图(Eulerian Graph) 包含欧拉回路的图称为欧拉图。
半欧拉图(Semi-Eulerian Graph) 包含欧拉路径但不包含欧拉回路的图称为半欧拉图。
欧拉图的判定条件 无向图 欧拉回路存在条件:当且仅当图是连通的,且每个顶点的度数都是偶数。
欧拉路径存在条件:当且仅当图是连通的,且恰好有两个顶点的度数是奇数(这两个顶点分别是路径的起点和终点)。
有向图 欧拉回路存在条件:当且仅当图是连通的,且每个顶点的入度等于出度。
欧拉路径存在条件:当且仅当图是连通的,且恰好有一个顶点的出度比入度大1(起点),恰好有一个顶点的入度比出度大1(终点),其余顶点的入度等于出度。
原码、反码、补码
原码:最高位(最左边一位)表示符号,0为正,1为负;其余位表示数值的绝对值。
反码:正数的反码与其原码相同,负数的反码为符号位为1,其余位与其原码按位取反。
补码:正数的补码与其原码相同,负数的补码为其反码加一。

评论

0 条评论,欢迎与作者交流。

正在加载评论...