C++学习笔记

        --------------------------------------------------------------------

1、根据给定的函数声明,实现函数功能,即在参数str所指向的字符串中搜索第一次出现字符c的位置。

char *func(const char *str, int c)

/*
    func   :    从给定字符串str中找到第一次匹配字符c的位置,并返回该指向的地址
    str    :    给定字符串
    c      :    要匹配的字符
    return :   成功,返回该位置的地址
                失败,返回空地址
*/
char *func(const char *str, int c)
{
    int i = 0;
    
    for (i = 0; str[i] != '\0'; i++)
        if (str[i] == c)
            return &str[i];
    return NULL;
}

 2、完成有头单项不循环链表的创建头节点以及插入数据节点(头插法 尾插法),自己定义结构体。

<llist.h>

#ifndef __LLIST_H__
#define __LLIST_H__

typedef struct ListNode {
    struct ListNode *next;
    char data[1];    
}LNode;

typedef struct {
    LNode head;
    size_t size;     // 数据节点节点域的大小
}List;

List *list_init(List **list, const size_t dataSize);

int list_head_insert(List *list, const void *data);

int list_tail_insert(List *list, const void *data);

#endif

<llist.c>

#include "llist.h"
#include <stdlib.h>
#include <string.h>

int list_init(List **list, const size_t dataSize)
{

    *list= malloc(sizeof(List));
    if (!*list) return -1;

    (*list)->head.next = NULL;
    (*list)->size = dataSize;   
    
    return 0;
}

static int __node_init(LNode **node, cosnt void *data, int size)
{
    *node = malloc(sizeof(LNode) + size);
    if (!*node) return -1;

    (*node)->next = NULL;
    memcpy(node->data, data, size);

    return 0;
}

int list_head_insert(List *list, const void *data)
{
    LNode *newnode = NULL;

    if (-1 == __node_init(&newnode, data, list->size)) 
        return -1;

    newnode->next = list->head.next;
    list->head.next = newnode;

    return 0;
}

int list_tail_insert(List *list, const void *data)
{
    LNode *newnode = NULL;
    LNode *cur = list->head;

    if (-1 == __node_init(&newnode, data, list->size)) 
        return -1;

    if (!cur->next) {
        list->head.next = newnode;
    } else {
        while (cur->next)
            cur = cur->next;
        cur->next = newnode;
    }

    return 0;
}

--------------------------------------------------------------------

 一、STL

        1、简介

                STL就是标准模板库的意思(Standard Template Library)

                STL的代码从广义上来讲分为三类:

                        【1】algorithm(算法):为容器提供了大量的操作,例如:排序\查找、、、

                        【2】container(容器):用来存储管理大量的数据,其实就是数据结构

                        【3】iterator(迭代器):架起容器和算法的桥梁,将容器与算法结合到一起

                STL标准模板库的实现就是采用了模板类和模板函数的方式,使其更加通用。

        2、手册

<array> 数组(实例化时指定长度)

<deque>

(double-ended queue)

双端队列
<forward_list> 单链表
<list> 双链表
<map> 类似于字典,处理一对一有关联的数组
<queue> 队列
<set> 集合,树结构
<stack>
<unordered_map> 无序的map
<unordered_set> 无序的set
<vector> 向量(可变长的数组)

        3、容器(Containers)

                1)分类

                        序列容器(线性)

                        array、vector、deque、forward_list、list

                        容器适配器

                        stack、queue

                        关联容器

                        set、map、pair、multiset、multimap

                        无序关联容器

                        unordered_set、unordered_map

                2)array(数组)

/*

        T:所包含元素的类型,别名为成员的类型:array::valur_type

        N:数组的大小,成员的个数

        other:

                array类是一个固定大小的序列容器,需要在初始化时,严格指定成员个数

*/

 template < class T, size_t N > class array;

/*

        ps:C++标准库新增加了begin()和end()这两个函数,与array容器包含的成员函数不同的是,标准库提供的这两个函数的操作对象,既可以是容器,也可以是普通数组。当操作对象是容器时,它和容器包含的成员函数的功能完全相同;如果操作对象是普通数组,则begin()函数返回的是数组第一个元素的指针,end()函数返回的是指向数组中最后一个元素之后一个位置的指针。

                在array头文件中,重载了get()全局函数,它可以访问容器中指定的元素,并返回该元素的引用。

                get<pos>(values)

*/

成员函数 功能
begin() 返回指向容器中第一个元素的随机访问迭代器。
end() 返回指向容器最后一个元素之后一个位置的随机访问迭代器,通常和 begin() 结合使用。
rbegin() 返回指向最后一个元素的随机访问迭代器。
rend() 返回指向第一个元素之前一个位置的随机访问迭代器。
cbegin() 和 begin() 功能相同,只不过在其基础上增加了 const 属性,不能用于修改元素。
cend() 和 end() 功能相同,只不过在其基础上,增加了 const 属性,不能用于修改元素。
crbegin() 和 rbegin() 功能相同,只不过在其基础上,增加了 const 属性,不能用于修改元素。
crend()
  • 24
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值