c_cpp C ++数据结构

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp C ++数据结构相关的知识,希望对你有一定的参考价值。

#inlcude <bdlb_tokenizer.h>
// https://bde.bloomberg.com/NonGit/Doxygen/bde_api_prod/
// Two kinds of delimiters:
//  Soft: adjacent delimiters are treated as one
//  Hard: adjacent deliieters are treated as multiple ones
bdld::Tokenizer tokenIt(line.c_str(), softDelims, hardDelims);
for ( ; tokenIt.isValid(); ++tokenIt) {
  bsl::cout << "token:" << tokenIt.token() << "\n";
}
### Reserve memory for vector:
```
bsl::vector.reserve(<number of elements>)
```

Other initiation methods:
```
bsl::vector<bsl::string> vs2(10); // size is 10, each
// string is empty
bsl::vector<bsl::string> vs3(5, "hello");
// size is 5, each string
// is "hello"
bsl::vector<bsl::string> vs4(vs3); // vs4 is copy of vs3
```

Be careful about size_t!
size_t is unsigned, doing arithmetic operation on it may be risky.


### ARRAY:
Initiation:
int evenNumbers[5] = { 0, 2, 4 }; // size 5, 0-filled
int zeros[10] = {0}; // size 10, all set to 0
## String
### Compare
```
string.compare(string)
//output: 0, <0, >0
```
### Initiation:
* The preferred way of initiate string is to using 
```
string hello{"hello"}
```
over
```
string hello("hello")
```

## Array
### best practice of passing an array to a function
```
void foo(int numbers[], size_t size);
```
### Pass multi-dimensional array to a function:
```
void reverse(int matrix[][WIDTH])
```
It is different from:
```
int *matrix[]
```
The former indicate the memory is continuosly allocated.
#include <bdeut_stringref.h>
void tokenize(bsl::vector<bsl::string>& tokens, 
                       const bsl::string& input, 
                       const char *softDelims,
                       const char *hardDelims)
{
    BloombergLP::bdeut_StrTokenRefIter tokenIt(input.c_str(), softDelims, hardDelims);
    for (; tokenIt; ++tokenIt) {
        tokens.push_back(tokenIt());
    }
}

以上是关于c_cpp C ++数据结构的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp C ++结构

c_cpp C - 结构

c_cpp 基本的c结构

c_cpp C程序的基本结构

c_cpp cpp c样式​​结构转换

c_cpp C中不可变数据框架的原型