我正在关注一本关于 C++ 编程的书,但我陷入了向量的困境。书中的例子是:
vector<int> v = {1,2,3};
Run Code Online (Sandbox Code Playgroud)
但我收到一个错误:
1 IntelliSense: no instance of constructor "Vector<T>::Vector [with T=int]" matches the argument list
argument types are: (int, int, int) ../path
Run Code Online (Sandbox Code Playgroud)
另外,当我创建字符串向量时:
vector<string> v = {"one", "two", "three"}
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
1 IntelliSense: no instance of constructor "Vector<T>::Vector [with T=std::string]" matches the argument list
argument types are: (const char [4], const char [4], const char [6]) ../path
Run Code Online (Sandbox Code Playgroud)
我正在使用 VS 2013 和 2013 年 11 月的 CTP 编译器。我究竟做错了什么?
有人可以解释一下为什么这段代码不会从向量中删除所有 1:
for (int i = 0; i < numbers.size(); i++)
{
if (numbers[i] == 1)
{
numbers.erase(numbers.begin() + i);
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个函数,该函数将从 a 返回最短的字符串vector<string>:
// Find the shortest string.
string shortestString(vector<string> v) {
string shortest;
int shortss = 0;
int i = 0;
for (string s : v) {
if (i = 0) {
shortss = s.length();
shortest = s;
i++;
}
else if (s.length() < shortss) {
shortss = s.length();
shortest = s;
}
}
return shortest;
}
Run Code Online (Sandbox Code Playgroud)
我不知道我是否犯了一些愚蠢的错误,但它什么也没返回。这是我的main():
int main() {
vector<string> words = { "a", "ab", "abc" };
string shor = shortestString(words);
cout << …Run Code Online (Sandbox Code Playgroud)