相关疑难解决方法(0)

对于字符串中的每个字符

如何在C++中对字符串中的每个字符执行for循环?

c++ loops for-loop character

208
推荐指数
6
解决办法
37万
查看次数

为什么不能"转换(s.begin(),s.end(),s.begin(),tolower)"成功编译?

鉴于代码:

#include <iostream>
#include <cctype>
#include <string>
#include <algorithm>
using namespace std;

int main()
{
     string s("ABCDEFGHIJKL");
     transform(s.begin(),s.end(),s.begin(),tolower);
     cout<<s<<endl;
}
Run Code Online (Sandbox Code Playgroud)

我收到错误:

呼叫没有匹配功能 transform(__gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, __gnu_cxx::__normal_iterator<char*, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >, <unresolved overloaded function type>)

什么是"未解决重载函数类型"是什么意思?

如果我用tolower我写的函数替换它,它不再是错误.

c++ compiler-errors lowercase toupper tolower

28
推荐指数
4
解决办法
2万
查看次数

错误:未在此范围内声明strcpy

我在Ubuntu g ++版本4.4.3中编译的c ++问题中遇到此问题.我不知道要包含的标题来解决这个问题..谢谢

centro_medico.cpp: In constructor ‘Centro_medico::Centro_medico(char*, char*, int, int, float)’:
centro_medico.cpp:5: error: ‘strcpy’ was not declared in this scope
centro_medico.cpp:13: warning: deprecated conversion from string constant to ‘char*’
centro_medico.cpp:13: warning: deprecated conversion from string constant to ‘char*’
centro_medico.cpp: In member function ‘Centro_medico& Centro_medico::operator=(const Centro_medico&)’:
centro_medico.cpp:26: error: ‘strcpy’ was not declared in this scope
centro_medico.cpp:39: warning: deprecated conversion from string constant to ‘char*’
centro_medico.cpp:39: warning: deprecated conversion from string constant to ‘char*’
centro_medico.cpp: In member function ‘bool Centro_medico::quitar_medico(int)’:
centro_medico.cpp:92: …
Run Code Online (Sandbox Code Playgroud)

c++ deprecated

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

循环c ++中的'冒号'和'自动'?需要一些帮助来理解语法

我需要对以下c ++语法进行一些解释:

for(const auto& ioDev : deviceList)
Run Code Online (Sandbox Code Playgroud)

鉴于:

std::vector<Device *> deviceList
Run Code Online (Sandbox Code Playgroud)

具体来说,我对':'和'auto'的使用感到困惑?

c++ c++11

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

对于utf8,boost :: algorithm :: to_upper/to_lower ok?boost :: locale没必要吗?

我在几个地方读到过boost :: algorithm :: to_upper/to_lower对utf8不好:

但是在我的系统上,ubuntu 12.4.1 32位,增强1.46,以及locale en_GB.UTF-8,一切看起来都很好,只要我通过语言环境例如:

std::locale englishUTF8locale("en_GB.UTF-8")
boost::algorithm::to_upper_copy(L"ü", englishUTF8locale) -> L"Ü"
boost::algorithm::to_lower_copy(L"?", englishUTF8locale) ->L"?"
Run Code Online (Sandbox Code Playgroud)

值得注意的是,它在使用std :: wstring时有效,但在使用std :: string时则无效

那么,boost :: locale是不是真的有必要?我的问题是我只能使用boost 1.46,而locale是为1.48创建的

c++ string boost internationalization

7
推荐指数
0
解决办法
2884
查看次数

在C++中将单个字符转换为小写 - tolower返回一个整数

我正在尝试将字符串转换为小写,并将其视为char*并迭代每个索引.问题是tolower我在网上读到的函数实际上并没有将char转换为小写:它将char作为输入并返回一个整数.

cout << tolower('T') << endl;
Run Code Online (Sandbox Code Playgroud)

116应该打印时打印到控制台T.

有没有更好的方法将字符串转换为小写?我在网上看过,大多数消息来源都说"使用tolower并遍历char数组",这对我来说似乎并不适用.

所以我的两个问题是:

  1. tolower当我打电话时,它使得它返回116而不是't' 的功能我做错了什么tolower('T')

  2. 除了tolower在每个单独的字符上使用之外,还有更好的方法将字符串转换为C++中的小写字母吗?

c++ string lowercase char tolower

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

如何编写自己的操纵器?

让我们假设我想编写自己的输入和输出操纵器.

cin >> mymanip >> str;
Run Code Online (Sandbox Code Playgroud)

要么

cout << mymanip << str;
Run Code Online (Sandbox Code Playgroud)

我希望mymanip做的是切换我从输入中读取的字符并将结果分配给一个字符串.

所以,如果我输入"QwErTy",我会在字符串中输入"qWeRtY".

这是一个非常基本的任务,只有一个功能,但我想了解更多关于操纵器的知识.

有人能给出一个线索吗?

谢谢.

c++ manipulators

4
推荐指数
2
解决办法
1001
查看次数

为什么我的代码会继续打印"i"而不是完整的句子?

我的工作是编写一个程序,将句子转换为大写和小写.

#include <iostream>
using namespace std;


int main ()
{
int current;
string sent;
cout << "Please enter a sentence: ";
cin >> sent;
for (int i = 0; i < sent.length(); i++)
{
    current = (int) sent[i];
    cout << "Your sentence in all lower case: "<< sent;
    cout << endl;
    if (current >= 65 && current <= 90)
    {
        current += 32;
        sent[i] = (char) current;
        cout << "Your sentence in all upper case: " << sent;
    }
    } …
Run Code Online (Sandbox Code Playgroud)

c++

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

C++ 将类型字符串变量中的单词转换为小写

为简单起见,我选择使用类型“字符串”而不是“字符”。但我应该小写我从输入文件中读入的字符串。我当时不知道我将无法使用“tolower()”。但是我确实找到了一种使用“转换”的方法。但是我无法让它工作,也找不到将它与一系列结构一起使用的示例。请帮忙。如果可能的话,我还必须将每个州的第一个字母大写,因此如果您能指出正确的方向,将不胜感激。

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <algorithm>

struct ATdata  
{
   string state;
   double miles;
   int shelters;
};

int readData( ifstream& input, struct ATdata data[] );
size_t readData( ifstream& input, struct ATdata data[], size_t MAX_ENTRIES );

int main()
{
    ifstream input;
    char filename[256];
    ATdata data[14];
    int i;

    cout << "Enter input file name: ";
    cin >> filename;

    input.open( filename );

    if ( input.fail() )
    {
        cout << "Input file does not exist." << endl;
        exit(1);
    }


    size_t …
Run Code Online (Sandbox Code Playgroud)

c++ string data-structures

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

C++代码优化

我创建了自定义函数将wstring转换为小写.但是,它在DebugMode中相当慢.是的,我知道ReleaseMode是重要的,但无论如何它是非常令人不安的.

wstring wstringToLower(wstring u)
{
    wstring s;

    for (int i=0;i<u.size();i++)
    {
        wstring sChar;
        sChar=u.substr(i,1);

        int iChar=static_cast<int>(sChar[0]);

        int iNewChar=charCodeToLower(iChar);

        wstring sNewChar=wstring(1,iNewChar);

        s.append(sNewChar);
    }

    return s;
}
Run Code Online (Sandbox Code Playgroud)

有没有人看到任何明显可以改进以加速代码的东西,即使在DebugMode中也是如此?

谢谢!

c++ optimization performance

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