Arrays 迭代器头:can';t使用begin或end with array作为参数,c++;11
1> c:\users\indira\documents\visual studio 2012\projects\cppremier\cppremier\ch1.cpp(2060):错误C3861:“开始”:找不到标识符 1> c:\users\indira\documents\visualstudio 2012\projects\cppremier\cppremier\ch1.cpp(2060):错误C3861:“结束”:找不到标识符 ======生成:0成功,1失败,0最新,0跳过==========Arrays 迭代器头:can';t使用begin或end with array作为参数,c++;11,arrays,pointers,iterator,Arrays,Pointers,Iterator,1> c:\users\indira\documents\visual studio 2012\projects\cppremier\cppremier\ch1.cpp(2060):错误C3861:“开始”:找不到标识符 1> c:\users\indira\documents\visualstudio 2012\projects\cppremier\cppremier\ch1.cpp(2060):错误C3861:“结束”:找不到标识符 ======生成:0成功,1失败,0最新,0跳过=====
#include <iostream>
#include <string>
#include <cstddef>
#include<array>
#include<vector>
#include <iterator>
using std::string;
using std::cin;
using std::cout;
using std::endl;
int main()
{
int ia[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int *pbeg = begin(ia), *pend = end(ia);
cout << "Elements in arr: " << endl;
for (int *pbeg; pbeg != pend; ++pbeg)
{
*pbeg = 0;
cout << *pbeg << endl;
}
getchar();
getchar();
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
使用std::string;
使用std::cin;
使用std::cout;
使用std::endl;
int main()
{
INTIA[]={0,1,2,3,4,5,6,7,8,9};
int*pbeg=开始(ia),*pend=结束(ia);
coutbegin
和end
是std
名称空间中的函数,因此您应该使用using
介绍它们,或者对它们进行限定(例如,作为std::begin
)
当您的参数本身是std
类型时,您不需要这样做:在这种情况下,依赖于参数的查找会找到它们-但是当您的参数是基元类型,或者在命名空间std
中未定义的任何内容时,ADL不起作用
这里有一些示例代码来解释在这种情况下名称查找是如何工作的。仅仅因为某些内容在翻译单元中可见,并不意味着它是有效的匹配项
namespace test {
struct Type { };
void foo(Type);
void bar(int);
}
int main() {
/*
Type t; <-- NO: Type is declared but not visible for name lookup
nl.cpp: In function ‘int main()’:
nl.cpp:11:5: error: ‘Type’ was not declared in this scope
nl.cpp:11:5: note: suggested alternative:
nl.cpp:3:12: note: ‘test::Type’
*/
test::Type u; // OK: the qualified name can be looked up
foo(u); // OK: argument-dependent lookup means test is considered
/*
bar(1); <-- NO: int isn't part of namespace test
nl.cpp:22:10: error: ‘bar’ was not declared in this scope
nl.cpp:22:10: note: suggested alternative:
nl.cpp:6:10: note: ‘test::bar’
*/
test::bar(1); // OK: explicitly tell the compiler where to look
}
名称空间测试{
结构类型{};
void-foo(类型);
空白条(int);
}
int main(){
/*
类型t;开始和结束函数在迭代器头中定义。它应该在没有std::begin的情况下工作。如果我将代码更改为仍然不工作:int*pbeg=std::begin(ia)是“未经初始化就使用”。它们在std名称空间的标题中定义。请注意,更改后,出现了不同的错误,我怀疑是在for循环中,您声明了一个新的未初始化pbeg
变量,该变量隐藏了现有变量。通过更正未初始化的问题,代码可以正常工作(根据您的建议,我还可以删除#include,因为它是从std名称空间访问的。)。但我仍然不明白迭代器头为什么不起作用。这里有一个链接,它显示这些函数是在这个头中定义的。所以最初的问题仍然困扰着我:使用迭代器头。