小编use*_*284的帖子

简单代码需要帮助 - 没有构造函数实例匹配参数列表

我正在关注一本关于 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 编译器。我究竟做错了什么?

c++ intellisense vector

5
推荐指数
1
解决办法
3811
查看次数

矢量擦除功能不起作用(简单代码)

有人可以解释一下为什么这段代码不会从向量中删除所有 1:

for (int i = 0; i < numbers.size(); i++)
{
    if (numbers[i] == 1)
    {
        numbers.erase(numbers.begin() + i);
    }
}
Run Code Online (Sandbox Code Playgroud)

c++

1
推荐指数
1
解决办法
1万
查看次数

返回最短的字符串

我正在尝试编写一个函数,该函数将从 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)

c++ string string-length

0
推荐指数
1
解决办法
4640
查看次数

标签 统计

c++ ×3

intellisense ×1

string ×1

string-length ×1

vector ×1