for(char c:s),std::vector<int> numbers 和std::int numbers[],.size()和.sizeof()区别

在C++中当需要对某个容器或数组进行遍历时我们可以使用以下语句,c将会被赋值为s中的元素

	for(char c:s):
	//s可以是任何满足条件的容器或数组
	
	for(int c:s):
	
	for(double c:s):
	
	for(float c:s):
	

在C++中我们来区分std::vector numbers = {1, 2, 3, 4, 5};和std::int numbers[] = {1, 2, 3, 4, 5};区别。

#include <iostream>
#include <vector>

int main() {
    std::vector<int> numbers = {1, 2, 3, 4, 5};

    // 使用范围-based for循环遍历vector<int>
    for (int num : numbers) {
        std::cout << num << " ";
    }

    return 0;
}
#include <iostream>

int main() {
    int numbers[] = {1, 2, 3, 4, 5};

    // 使用范围-based for循环遍历数组
    for (int num : numbers) {
        std::cout << num << " ";
    }

    return 0;
}

区别:

动态大小 vs. 静态大小:
std::vector numbers = {1, 2, 3, 4, 5};:std::vector是

In file included from main.cpp:11: solution.cpp: In member function 'std::vector<std::__cxx11::basic_string<char> > Solution::GetTop3App(const std::vector<std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, int> >&, const std::vector<std::__cxx11::basic_string<char> >&, const std::vector<std::__cxx11::basic_string<char> >&)': solution.cpp:22:75: error: no matching function for call to 'find(std::vector<std::__cxx11::basic_string<char> >::const_iterator, std::vector<std::__cxx11::basic_string<char> >::const_iterator, std::__tuple_element_t<1, std::tuple<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, int> >&)' if(find(selectedUsers.begin(), selectedUsers.end(), get<1>(it)) != selectedUsers.end()) { ^ In file included from /usr/include/c++/8/bits/locale_facets.h:48, from /usr/include/c++/8/bits/basic_ios.h:37, from /usr/include/c++/8/ios:44, from main.cpp:7: /usr/include/c++/8/bits/streambuf_iterator.h:368:5: note: candidate: 'template<class _CharT2> typename __gnu_cxx::__enable_if<std::__is_char<_CharT2>::__value, std::istreambuf_iterator<_CharT> >::__type std::find(std::istreambuf_iterator<_CharT>, std::istreambuf_iterator<_CharT>, const _CharT2&)' find(istreambuf_iterator<_CharT> __first, ^~~~ /usr/include/c++/8/bits/streambuf_iterator.h:368:5: note: template argument deduction/substitution failed: In file included from main.cpp:11: solution.cpp:22:75: note: '__gnu_cxx::__normal_iterator<const std::__cxx11::basic_string<char>*, std::vector<std::__cxx11::basic_string<char> > >' is not derived from 'std::istreambuf_iterator<_CharT>' if(find(selectedUsers.begin(), selectedUsers.end(), get<1>(it)) != selectedUsers.end()) { ^ solution.cpp:27:97: error: a function-definition is not allowed here before '{' token bool sortCpuFirst(tuple<string, string, int, int> a, tuple<string, string, int, int> b) { ^ solution.cpp:35:97: error: a function-definition is not allowed here before '{' token bool sortMemFirst(tuple<string, string, int, int> a, tuple<string, string, int, int> b) {
最新发布
11-12
Line 7: Char 9: ================================================================= ==22==ERROR: AddressSanitizer: heap-buffer-overflow on address 0x502000000354 at pc 0x55de9091eda9 bp 0x7ffeb85c4260 sp 0x7ffeb85c4258 READ of size 4 at 0x502000000354 thread T0 #0 0x55de9091eda8 in swap<int> /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/bits/move.h:222:13 #1 0x55de9091eda8 in iter_swap<__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > > /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/bits/stl_algobase.h:185:7 #2 0x55de9091eda8 in void std::__reverse<__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int>>>>(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int>>>, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int>>>, std::random_access_iterator_tag) /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/bits/stl_algo.h:1062:4 #3 0x55de9091ec00 in reverse<__gnu_cxx::__normal_iterator<int *, std::vector<int, std::allocator<int> > > > /usr/lib/gcc/x86_64-linux-gnu/14/../../../../include/c++/14/bits/stl_algo.h:1089:7 #4 0x55de9091ec00 in Solution::rotate(std::vector<int, std::allocator<int>>&, int) solution.cpp:7:9 #5 0x55de9091e4ad in __helper__ solution.cpp:7:18 #6 0x55de9091e4ad in main solution.cpp:7:30 #7 0x7f3b1408a1c9 (/lib/x86_64-linux-gnu/libc.so.6+0x2a1c9) (BuildId: 6d64b17fbac799e68da7ebd9985ddf9b5cb375e6) #8 0x7f3b1408a28a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28a) (BuildId: 6d64b17fbac799e68da7ebd9985ddf9b5cb375e6) #9 0x55de90847f44 in _start (solution+0xb2f44) 0x502000000354 is located 0 bytes after 4-byte region [0x502000000350,0x502000000354) allocated by thread T0 here: #0 0x55de9091bb6d in operator new(unsigned long) /root/llvm-project/compiler-rt/lib/asan/asan_new_delete.cpp:86:3 #1 0x55de909381de i
03-09
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值