C++ self-learning notes(3)

1. auto

Firstly, auto can be used to automatically determin the types of values. However, when you use the keyword auto, you have to initialize the variables (i.e. you have to assign an initial value to the variables immediately after you define the variables).

EXAMPLE:

122252_38vJ_2968040.png         122339_N2Fq_2968040.png

However, the example above is not appropriate since this kind of defining operate actually it not very clear. The keyword auto can be very helpful when you use it to replace some over complicated expression.

EXAMPLE:

133438_sq0H_2968040.png

133547_u3gZ_2968040.png

Comparing the two outputing module, you can see how the keyword auto can help you coding.

Moreover, the keyword auto can also be helpful when we call templates. However, I don't know much about templates right now. I'll update this blog when I learn more about templates.

 

Useful links:

http://blog.csdn.net/huang_xw/article/details/8760403

http://blog.csdn.net/yhl_leo/article/details/50864612

http://www.cnblogs.com/QG-whz/p/4951177.html#_label2

http://en.cppreference.com/w/cpp/language/auto

 

2. vector 

Vector is suppose to replace array in C. Vectors are able to store any types of data dynamically. 

Be carefull:

1. avoid to define your vector too long.

2. usually, when you define a vector, the command goes: vector<type>name. However, if you want to define a vector as an input parameter, the command goes: int func( vector<type>&name) 

vector operations:

1) you must include the head file <vector>;

2) create a vector object: vector<type> name;

3) insert element at the end of the vector: vec.push_back(element);

4) if you want to visit the ith element: vec[i], (remember the vector indexes starts from 0);

5) you can ues the iterator to visit elements in the vector: 

    vector<type>::iterator i;

    for ( i = vec.begin(); i != vec.end(); i++) { cout<<*i<<endl;}

6) insert an element in the middle of the vector:  vec.insert( vec.begin()+i,a); 

7) delete an element: vec.erase(vec.begin()+i); this command deletes the (i+1)th element;

 delet multiple elements: vec.erase(vec.begin()+i, vec.end()+j); this command deletes all the elements between vec[i] and vec[j];

8) size of the vector: vec.size();

9) clear the vector: vec.clear();

Useful links:

http://blog.csdn.net/duan19920101/article/details/50617190

http://www.cnblogs.com/wxmdevelop/p/4600762.html

http://blog.csdn.net/hancunai0017/article/details/7032383

http://blog.csdn.net/qq_15437667/article/details/52215917

 

3. uint8_t/uint16_t/uint32_t/uint64_t ......

Acctually, these data types are not new. They are only existed data types renamed using the keyword typedef. 

int8_t      corresponds to     signed char

int16_t    corresponds to     short int

int32_t    corresponds to     int

int64_t    corresponds to     long int  or long long int ( this part confuses me too....)

uint8_t    corresponds to     unsigned char

uint16_t  corresponds to     unsigned short int

uint32_t  corresponds to     unsigned int

uint64_t  corresponds to     unsigned long int or long long int (also confused)

Useful links:

http://blog.sina.com.cn/s/blog_9dcc0fb90101gdvo.html

http://blog.csdn.net/kiddy19850221/article/details/6655066

转载于:https://my.oschina.net/u/2968040/blog/994646

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的Q-learning算法的C++代码示例,帮助你入门: ``` #include <iostream> #include <cmath> #include <ctime> #include <cstdlib> using namespace std; const int NUM_STATES = 6; // 状态数量 const int NUM_ACTIONS = 2; // 动作数量 const double GAMMA = 0.8; // 折扣因子 const double ALPHA = 0.1; // 学习率 const int MAX_EPISODES = 13; // 最大迭代次数 int R[NUM_STATES][NUM_ACTIONS] = { {0, 0}, {0, 100}, {0, 0}, {100, 0}, {0, 0}, {0, 0} }; // 奖励矩阵 int Q[NUM_STATES][NUM_ACTIONS] = {0}; // Q值矩阵 int choose_action(int state) { // 选择动作 int action; if (rand() / (double)RAND_MAX < 0.8) { // 以80%的概率选择Q值最大的动作 int maxQ = -1; for (int i = 0; i < NUM_ACTIONS; i++) { if (Q[state][i] > maxQ) { maxQ = Q[state][i]; action = i; } } } else { // 以20%的概率随机选择动作 action = rand() % NUM_ACTIONS; } return action; } void q_learning() { // Q-learning算法 for (int i = 0; i < MAX_EPISODES; i++) { int state = rand() % NUM_STATES; while (state != 5) { // 当状态不是终止状态时 int action = choose_action(state); int next_state; if (action == 0) { next_state = state - 1; } else { next_state = state + 1; } int reward = R[state][action]; Q[state][action] = (1 - ALPHA) * Q[state][action] + ALPHA * (reward + GAMMA * max(Q[next_state][0], Q[next_state][1])); // 更新Q值 state = next_state; } } } int main() { srand(time(NULL)); q_learning(); cout << "Q values:" << endl; for (int i = 0; i < NUM_STATES; i++) { for (int j = 0; j < NUM_ACTIONS; j++) { cout << Q[i][j] << " "; } cout << endl; } return 0; } ``` 以上代码实现了一个简单的Q-learning算法,用于解决一个简单的迷宫问题。其中,R矩阵为奖励矩阵,Q矩阵为Q值矩阵,choose_action函数用于选择动作,q_learning函数用于执行Q-learning算法。在main函数中,我们调用q_learning函数,然后打印出Q值矩阵的值。 请注意,此示例程序仅用于演示Q-learning的基本思想,实际应用中需要根据具体问题进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值