Zheng Chu's Blog

让希望永驻


  • 主页

  • 所有专栏

  • 历史文章

  • 标签

  • 关于我

CookingC++

Posted on 2019-09-15 Edited on 2020-12-06 In C++ Views:

把一个array拷贝到vector

其中的copy方法里,copy(&dataArray[0], &dataArray[dataArraySize], back_inserter(dataVec)); 不会拷贝dataArray[dataArraySize] 元素;例子如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
int arr[] = {1,4,2,4,56,5,43,2,2,5};
int main()
{
int size = sizeof(arr) / sizeof(int);
vector<int> table;
copy(&arr[0], &arr[3], back_inserter(table));
for (auto i : table)
cout << i << " ";
cout << endl;
}
//打印到arr[2]处为止;
$main
1 4 2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
vector<int> dataVec;

int dataArray[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
unsigned dataArraySize = sizeof(dataArray) / sizeof(int);

// Method 1: Copy the array to the vector using back_inserter.
{
copy(&dataArray[0], &dataArray[dataArraySize], back_inserter(dataVec));
}

// Method 2: Same as 1 but pre-extend the vector by the size of the array using reserve
{
dataVec.reserve(dataVec.size() + dataArraySize);
copy(&dataArray[0], &dataArray[dataArraySize], back_inserter(dataVec));
}

// Method 3: Memcpy
{
dataVec.resize(dataVec.size() + dataArraySize);
memcpy(&dataVec[dataVec.size() - dataArraySize], &dataArray[0], dataArraySize * sizeof(int));
}

// Method 4: vector::insert
{
dataVec.insert(dataVec.end(), &dataArray[0], &dataArray[dataArraySize]);
}

// Method 5: vector + vector
{
vector<int> dataVec2(&dataArray[0], &dataArray[dataArraySize]);
dataVec.insert(dataVec.end(), dataVec2.begin(), dataVec2.end());
}

按符号切割字符串

1
2
3
std::string s = "scott>=tiger";
std::string delimiter = ">=";
std::string token = s.substr(0, s.find(delimiter)); // token is "scott"
  • The find(const string& str, size_t pos = 0) function returns the position of the first occurrence of str in the string, or npos if the string is not found.
  • The substr(size_t pos = 0, size_t n = npos) function returns a substring of the object, starting at position pos and of length npos.

删除字符串中的空白符号

1
s.erase(remove_if(s.begin(), s.end(),::isspace), s.end());
# Cooking
累加累减容器中的元素
Strings
  • Table of Contents
  • Overview
Zheng Chu

Zheng Chu

90 posts
20 categories
25 tags
GitHub 简书 CSDN E-Mail
  1. 1. 把一个array拷贝到vector
  2. 2. 按符号切割字符串
  3. 3. 删除字符串中的空白符号
© 2021 Zheng Chu
Powered by Hexo v4.2.1
|
Theme – NexT.Pisces v7.3.0
|