2018.02.29 leaning journal

本文详细介绍了C++编程中的几个核心概念:如何使用string::substr方法截取字符串;getline函数的多种用法及其应用场景;map容器的工作原理及其实现方式;以及C++中的模板应用示例。这些内容对于初学者理解和掌握C++编程语言的基础非常有帮助。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

C++语法查漏补缺

越来越发现自己编程和数学基础薄弱了,羞愧死!!!!!!!!!!!!!!!!!!

1.string::substr

public member function       <string>
std::string::substr

string substr (size_t pos = 0, size_t len = npos) const;

Generate substring
Returns a newly constructed string object with its value initialized to a copy of a substring of this object.
The substring is the portion of the object that starts at character position pos and spans len characters (or until the end of the string, whichever comes first).

子字符串substring是从字符串开始位置pos并跨越长度为len的字符(或直到字符串结尾,以先到者为准)的对象的局部。

2.getline函数

std::getline (string)  C++11
(1) 
istream& getline (istream&  is, string& str, char delim);
istream& getline (istream&& is, string& str, char delim);
(2) 
istream& getline (istream&  is, string& str);
istream& getline (istream&& is, string& str);

Get line from stream into string 从文件流中获取行到字符串
Extracts characters from is and stores them into str until the delimitation character delim is found (or the newline character, ‘\n’, for (2)).
The extraction also stops if the end of file is reached in is or if some other error occurs during the input operation.
If the delimiter is found, it is extracted and discarded (i.e. it is not stored and the next input operation will begin after it).
Note that any content in str before the call is replaced by the newly extracted sequence.
Each extracted character is appended to the string as if its member push_back was called.

is中提取字符并将它们存储到str中,直到找到定界字符定界符(或(2)的换行符'\n')。
如果到达文件末尾或者在输入操作过程中发生其他错误,则提取也会停止。
如果找到分隔符,它将被提取并丢弃(即它不被存储并且下一个输入操作将在它之后开始)。
请注意,在调用之前str中的任何内容都被新提取的序列替换。
每个提取的字符都会附加到该字符串,就像调用其成员push_back一样。

3.map

std::map
template < class Key,                                     // map::key_type
           class T,                                       // map::mapped_type
           class Compare = less<Key>,                     // map::key_compare
           class Alloc = allocator<pair<const Key,T> >    // map::allocator_type
           > class map;
Maps are associative containers that store elements formed by a combination of a key value
and a mapped value, following a specific order.
In a map, the key values are generally used to sort and uniquely identify the elements, 
while the mapped values store the content associated to this key. The types of key and 
mapped value may differ, and are grouped together in member type value_type, which is a pair
type combining both:

typedef pair<const Key, T> value_type;

map是一种关联容器,按照特定顺序存储由key值和value值组合形成的元素。

在map中,key通常用于对元素进行排序和唯一标识,而value存储与此key关联的内容。key和value的类型可能不同,并且在成员类型value_type中组合在一起,这是一种结合了以下两者的成对的类型。

public:
        map<string,string>data;

   string key = str.substr( 0, pos );
   string value = str.substr( pos+1, str.length() );
   data[key] = value;

4.模板

segnet-深度学习

caffe-C++

ecilipse-C++

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值